https://kotlinlang.org logo
Title
s

Sourabh Rawat

12/15/2021, 3:44 PM
Hi, any idea how to handle a
Flow
of
Either
? I want to do a
fold
on it as a terminal operator.
s

simon.vergauwen

12/15/2021, 5:06 PM
Do you want to short-circuit the
Flow
if you encounter
Either.Left
? Otherwise you could write:
suspend fun <E, A> Flow<Either<E, A>>.fold(left: suspend (E) -> Unit, right: suspend (A) -> Unit) : Unit =
  collect { either ->
   either.fold({ left(it) }, { right(it) })
  }
s

Sourabh Rawat

12/16/2021, 4:32 AM
I want to short-circuit. Right now, I doing something like
either {
  lst.asFlow()
     .map { callServiceReturningEither(it) }
     .fold(initAcc) { acc, eitherRes -> acc.combine(eitherRes.bind()) } 
}
I am not sure if this will short circuit properly.
s

simon.vergauwen

12/16/2021, 9:17 AM
This will short-circuit correctly 👍 Upon the first
Either.Left.bind()
inside
fold
it will short-circuit the `fold`/`Flow#collect` and it’ll return the encountered
Either.Left
.
:thank-you: 1