arrow-fx-coroutines / arrow.fx.coroutines.stream / scan

scan

fun <O, O2> Stream<O>.scan(init: O2, f: (O2, O) -> O2): Stream<O2>

Left fold which outputs all intermediate results.

import arrow.fx.coroutines.stream.*

//sampleStart
suspend fun main(): Unit =
  Stream(1,2,3,4)
  .scan(0) { a, b -> a + b }
  .toList()
  .let(::println) // [0, 1, 3, 6, 10]
//sampleEnd

More generally: Stream().scan(z, f) == Stream(z) Stream(x1).scan(z, f) == Stream(z, f(z,x1)) Stream(x1,x2).scan(z, f) == Stream(z, f(z,x1), f(f(z,x1),x2)) etc

Do you like Arrow?

Arrow Org
<