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

buffer

fun buffer(n: Int): Stream<O>

Behaves like the identity function, but requests n elements at a time from the input.

import arrow.fx.coroutines.stream.*

//sampleStart
suspend fun main(): Unit {
  val buf = mutableListOf<String>()

  Stream.range(0..100)
    .effectTap { i -> buf.add(">$i") }
    .buffer(4)
    .effectTap { i -> buf.add("<$i") }
    .take(10)
    .toList()
    .let(::println) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

  println(buf.joinToString()) // [>0, >1, >2, >3, <0, <1, <2, <3, >4, >5, >6, >7, <4, <5, <6, <7, >8, >9, >10, >11, <8, <9]
}
//sampleEnd

Do you like Arrow?

Arrow Org
<