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

bufferBy

fun bufferBy(f: (O) -> Boolean): Stream<O>

Behaves like the identity stream, but requests elements from its input in blocks that end whenever the predicate switches from true to false.

import arrow.fx.coroutines.stream.*

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

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

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

Do you like Arrow?

Arrow Org
<