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

attempt

fun attempt(): Stream<Either<Throwable, O>>

Returns a stream of O values wrapped in Either.Right until the first error, which is emitted wrapped in Either.Left.

import arrow.fx.coroutines.stream.*

//sampleStart
suspend fun main(): Unit =
  Stream(1, 2, 3)
    .append { Stream.raiseError(RuntimeException("Boom!")) }
    .append { Stream(4, 5, 6) }
    .attempt()
    .toList()
    .let(::println) // [Right(b=1), Right(b=2), Right(b=3), Left(a=java.lang.RuntimeException: Boom!)]
//sampleEnd

Do you like Arrow?

Arrow Org
<