arrow-fx-coroutines / arrow.fx.coroutines.stream / firstOrNull

firstOrNull

fun <O> Pull<O, Unit>.firstOrNull(f: (O) -> Boolean): Pull<Nothing, PullUncons1<O>?>

Awaits the next available element where the predicate returns true.

suspend fun <O> Stream<O>.firstOrNull(): O?

Runs the first effect of this Stream, and returns null if the stream emitted a value and returns the value if emitted.

import arrow.fx.coroutines.stream.*

//sampleStart
suspend fun main(): Unit =
  Stream.range(0..1000)
    .firstOrNull()
    .let(::println) // 0
//sampleEnd

This a terminal operator, meaning this functions suspends until the Stream finishes. If any errors are raised while streaming, it’s thrown from this suspend scope.

Do you like Arrow?

Arrow Org
<