does arrow have any integration with flows, or do ...
# arrow
b
does arrow have any integration with flows, or do i have to translate to an exception like this:
Copy code
flow {
    while(true) {
        when(val r = functionThatReturnsEither()){
            is Right -> emit(r.value)
            is Left -> error("some error")
        }
    }
}
d
Couldn't you use
fold
?
Copy code
functionThatReturnsEither()
    .fold(
      { error("some error") },
      { emit(it) }, 
    )
b
that's just different syntax for the same thing, i'm asking if i need to translate from typed errors to exceptions
d
It depends on what you want to happen. I'm not really sure I understand what you're hoping for, can you write an example of what you would want it to look like?
b
i want the flow to cancel with an error when
functionThatReturnsEither
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:
Copy code
flow {
    while(true){
        // cancels flow with exception
        functionThatThrows()
    }
}
.catch {t -> /* exception here */ }
.collect()
but with a function that returns
Either
instead of throwing an exception
d
Maybe something like
getOrElse { cancel() }
b
that also translates the typed error to an exception, which i guess is the only option
d
getOrElse { break }
?
b
you lose the error information
d
Where do you want the error information to be used?
b
in the
catch
function
d
Ah, so if you emit the either, use
Flow.map
b
yea i probably have to emit the either and then handle it outside, or use an exception to cancel it... i guess i was just looking to see if there was already a standard way people did this type of thing
d
It wouldn't be hard to create your own extension method:
Copy code
inline fun <E, V> Flow<Either<E, V>>.onLeft(crossinline handle: (E) -> Unit) =
    transformWhile {
        it.fold(
            {
                handle(it)
                false
            },
            {
                emit(it)
                true
            },
        )
    }
Not sure if arrow has something like that already though.
b
i couldn't find anything in the docs, only thing that's missing is stopping the flow
d
That's what the
false
here does.
b
ah
b
haven't used that one before
that's pretty good, behavior parallels the
catch
with exceptions.