Skip to content

Instantly share code, notes, and snippets.

@fteychene
Created April 2, 2019 08:30
Show Gist options
  • Save fteychene/02f035bdc134e6f44590d727e41b2c8e to your computer and use it in GitHub Desktop.
Save fteychene/02f035bdc134e6f44590d727e41b2c8e to your computer and use it in GitHub Desktop.
Kotlin currified
fun <P1, P2, R> ((P1, P2) -> R).curried(): (P1) -> (P2) -> R = { p1: P1 -> { p2: P2 -> this(p1, p2) } }
fun <P1, P2, P3, R> ((P1, P2, P3) -> R).curried(): (P1) -> (P2) -> (P3) -> R = { p1: P1 -> { p2: P2 -> { p3: P3 -> this(p1, p2, p3) } } }
fun <P1, P2, P3, P4, R> ((P1, P2, P3, P4) -> R).curried(): (P1) -> (P2) -> (P3) -> (P4) -> R = { p1: P1 -> { p2: P2 -> { p3: P3 -> { p4: P4 -> this(p1, p2, p3, p4) } } } }
fun <P1, P2, P3, P4, P5, R> ((P1, P2, P3, P4, P5) -> R).curried(): (P1) -> (P2) -> (P3) -> (P4) -> (P5) -> R = { p1: P1 -> { p2: P2 -> { p3: P3 -> { p4: P4 -> { p5: P5 -> this(p1, p2, p3, p4, p5) } } } } }
fun test(val1: Int, val2: String, val3: Float, val4: Double): String = "$val1, $val2, $val3, $val4"
val currified = ::test.curried() // (val1: Int) -> (val2: String) -> (val3: Float) -> (val4: Double) -> String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment