arrow-fx-coroutines / arrow.fx.coroutines.stream / Stream / unfold

unfold

fun <S, O> unfold(s: S, f: (S) -> Pair<O, S>?): Stream<O>

Creates a stream by successively applying f until a null is returned, emitting each output O and using each output S as input to the next invocation of f.

import arrow.fx.coroutines.stream.*

//sampleEnd
suspend fun main(): Unit =
  Stream.unfold(0) { i -> if (i < 5) Pair(i, i + 1) else null }
    .toList()
    .let(::println) //[0, 1, 2, 3, 4]
//sampleStart

Do you like Arrow?

Arrow Org
<