Foggy day

Kotlin - lambda expression -4 : curried function 본문

Kotlin

Kotlin - lambda expression -4 : curried function

jinhan38 2021. 3. 21. 15:06

 

class KotlinPlayGroundActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin_play_ground)

        val a = "A"
        val b = "B"
        val c = "C"
        val d = "D"

        println("func : ${func(a, b, c, d)}")
        println("curriedFunc : ${curriedFunc<String, String, String, String>()(b)(c)(d)(a)}")
        println("curriedString : ${curriedString(a)(b)(c)(d)}")

    }

    fun <A, B, C, D> func(a: A, b: B, c: C, d: D): String = "$a, $b, $c, $d"

    fun <A, B, C, D> curriedFunc() =
        { a: A ->
            { b: B ->
                { c: C ->
                    { d: D ->
                        "$a, $b, $c, $d"
                    }
                }

            }
        }

    val curriedString: curriedString = { a ->
        { b ->
            { c ->
                { d ->
                    "$a, $b, $c, $d"
                }
            }
        }
    }


}


typealias curriedString = (String) -> (String) -> (String) -> (String) -> String