Last active
November 7, 2017 19:19
-
-
Save Pooh3Mobi/9795ff2693b3eeda42de3a58a1701811 to your computer and use it in GitHub Desktop.
Currying at Kotlin Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
// x + y | |
val sum2Ints : (Int, Int) -> Int = { x, y -> x + y } | |
val curriedSum2Ints = sum2Ints.curried() | |
val applied1IntSum2Ints = curriedSum2Ints(5) | |
println(applied1IntSum2Ints(8)) // 13 | |
// x + y + z | |
val sum3Ints : (Int, Int, Int) -> Int = { x, y, z -> x + y + z } | |
val curriedSum3Ints = sum3Ints.curried() | |
val applied2IntsSum3Ints = curriedSum3Ints(1)(2) | |
println(applied2IntsSum3Ints(3)) // 6 | |
println(applied2IntsSum3Ints(4)) // 7 | |
} | |
// currying functions. | |
// see https://github.com/MarioAriasC/funKTionale/blob/master/funktionale-currying/src/main/kotlin/org/funktionale/currying/namespace.kt | |
fun <P1, P2, R> ((P1, P2) -> R).curried() : (P1) -> (P2) -> R { | |
return { p1: P1 -> { p2: P2 -> this(p1, p2) } } | |
} | |
fun <P1, P2, P3, R> ((P1, P2, P3) -> R).curried() : (P1) -> (P2) -> (P3) -> R { | |
return { p1: P1 -> { p2: P2 -> { p3: P3 -> this(p1, p2, p3) } } } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment