Is there a stdlib way to consecutively apply a lis...
# stdlib
s
Is there a stdlib way to consecutively apply a list of functions to a single variable and their outputs?
s
This sounds like you want function composition
s
I’m seeing this example on Kotlin’s site, but I’m not sure how to extend it to an arbitrarily long list of functions: https://try.kotlinlang.org/#/Examples/Callable%20references/Composition%20of%20functions/Composition%20of%20functions.kt
s
one of the more #functional guys might be able to explain it better
but I think
.fold { ... }
might actually work in this situation
might look something like this:
Copy code
fun a(i: Int): Int = i + 1
fun b(i: Int): Int = i * 2
fun c(i: Int): Int = i + 3

fun test() {
  val start = 1
  val fns = listOf(::a, ::b, ::c)
  val final = fns.fold(start) { acc, consumer -> consumer(acc) }
}
s
ah this looks perfect!
👍 1
I looked at
fold
, but it didn’t occur to me to use a lambda like that