Skip to content

Instantly share code, notes, and snippets.

@KucherenkoIhor
Last active February 8, 2018 06:56
Show Gist options
  • Save KucherenkoIhor/95e1c5e65404d45368490f293dd53ffb to your computer and use it in GitHub Desktop.
Save KucherenkoIhor/95e1c5e65404d45368490f293dd53ffb to your computer and use it in GitHub Desktop.
compising of functions with different types
fun length(str: String): Int = str.length
fun strPlusX(x: Int): String = "string " + x
fun add6To(x: Int): Int = x + 6
fun xMultipleX(x: Int): Int = x * x
operator fun <P1, R1, R2> ((R1) -> R2).plus(f: (P1) -> R1): (P1) -> R2 {
return {p1: P1 -> this(f(p1)) }
}
fun main(vars: Array<String>) {
val composed = ::length + ::strPlusX + ::add6To + ::xMultipleX // = ('string' + ((6 * 6) + 6)).length
println(composed(6)) // 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment