bj0
11/18/2024, 6:18 PMflow {
while(true) {
when(val r = functionThatReturnsEither()){
is Right -> emit(r.value)
is Left -> error("some error")
}
}
}
Daniel Pitts
11/18/2024, 8:11 PMfold
?
functionThatReturnsEither()
.fold(
{ error("some error") },
{ emit(it) },
)
bj0
11/18/2024, 10:01 PMDaniel Pitts
11/18/2024, 10:02 PMbj0
11/18/2024, 10:08 PMfunctionThatReturnsEither
returns a typed error. In that snippet i'm doing it manually by converting to an exception, I was wondering if there's any integration/api that does it without that. I'm basically looking for the arrow version of:
flow {
while(true){
// cancels flow with exception
functionThatThrows()
}
}
.catch {t -> /* exception here */ }
.collect()
but with a function that returns Either
instead of throwing an exceptionDaniel Pitts
11/18/2024, 10:14 PMgetOrElse { cancel() }
bj0
11/18/2024, 10:17 PMDaniel Pitts
11/18/2024, 10:18 PMgetOrElse { break }
?bj0
11/18/2024, 10:19 PMDaniel Pitts
11/18/2024, 10:19 PMbj0
11/18/2024, 10:20 PMcatch
functionDaniel Pitts
11/18/2024, 10:22 PMFlow.map
bj0
11/18/2024, 10:23 PMDaniel Pitts
11/18/2024, 10:34 PMinline fun <E, V> Flow<Either<E, V>>.onLeft(crossinline handle: (E) -> Unit) =
transformWhile {
it.fold(
{
handle(it)
false
},
{
emit(it)
true
},
)
}
Daniel Pitts
11/18/2024, 10:35 PMbj0
11/18/2024, 10:36 PMDaniel Pitts
11/18/2024, 10:36 PMfalse
here does.bj0
11/18/2024, 10:36 PMDaniel Pitts
11/18/2024, 10:36 PMbj0
11/18/2024, 10:37 PMbj0
11/18/2024, 10:39 PMcatch
with exceptions.