Skip to content

Instantly share code, notes, and snippets.

@hisui
Created June 9, 2016 02:09
Show Gist options
  • Save hisui/7d429f9787716d303ae5acb3c97dd288 to your computer and use it in GitHub Desktop.
Save hisui/7d429f9787716d303ae5acb3c97dd288 to your computer and use it in GitHub Desktop.
class Human(val name: String, val age: Int, val children: List<Human>)
val age: Int? = null
print(::Human.curry() `$` "John" `*` age `*` listOf<Human>()) // == null
print(::Human.curry() `$` "Mary" `*` 999 `*` listOf<Human>()) // != null
fun<A, B, Z> Function2<A, B, Z>.curry(): (A) -> (B) -> Z {
return { a: A -> { b: B -> this(a, b) } }
}
fun<A, B, C, Z> Function3<A, B, C, Z>.curry(): (A) -> (B) -> (C) -> Z {
return { a: A -> { b: B -> { c: C -> this(a, b, c) } } }
}
infix fun<A, B> Function1<A, B>?.`*`(a: A?): B? = this?.let { f -> a?.let(f) }
infix fun<A, B> Function1<A, B> .`$`(a: A?): B? = this `*` a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment