dio
07/20/2017, 4:35 PMfun foo(x: Int): Int
fun bar(x: Int): Int
fun baz(x: Int): Int
I could baz(bar(foo(5)))
but that starts to feel nested and gross, in clojure I can say (-> 5 foo bar baz)
to feed the results of one function call into the next and un-nest my calls, I'm wondering if there's some equivalent ?hho
07/20/2017, 5:01 PMfun foo(input: Int) = 2 * input
fun bar(input: Int) = input + 2
fun baz(input: Int) = input / 2
fun <T> applyComposite(start: T, vararg functions: (T) -> T) : T {
var result = start
functions.forEach { result = it.invoke(result) }
return result
}
fun main(args: Array<String>) {
applyComposite(5, ::foo, ::bar, ::baz)
}
dio
07/20/2017, 5:02 PM