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

unfoldChunk

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

Like unfold but each invocation of f provides a chunk of output.

import arrow.fx.coroutines.stream.*

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

Do you like Arrow?

Arrow Org
<