Hi, any idea how to handle a `Flow` of `Either` ?...
# functional
s
Hi, any idea how to handle a
Flow
of
Either
? I want to do a
fold
on it as a terminal operator.
s
Do you want to short-circuit the
Flow
if you encounter
Either.Left
? Otherwise you could write:
Copy code
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
I want to short-circuit. Right now, I doing something like
Copy code
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
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
.
🙏 1