https://kotlinlang.org logo
#arrow
Title
# arrow
f

Fred Friis

03/04/2024, 9:55 PM
how do you filter and replace a given result with error in a Kotlin Result and Arrow Either? eg given the types Result<Response> or Either<Throwable, Response> if the response in the success case/on the right has response.isSuccessful is false, I want to turn that into a failed result/throwable on the left
d

dave08

03/05/2024, 5:30 AM
I think you can bind() a result in an either scope?
l

Laurent Thiebaud

03/05/2024, 8:17 AM
what about
.map()
functions in Arrow ?
r

raulraja

03/05/2024, 9:08 AM
scratch.kt
You could use
bind
or
flatMap
like in the snippet above.
Or use the Raise DSL to create your own operations for the
either
block.
a

Alejandro Serrano.Mena

03/05/2024, 9:13 AM
the simples way would be to do:
Copy code
fun <A> Either<Throwable, A>.toResult() = Result<A> = either { this.bind() }

fun <A> Result<A>.toEither(): Either<Throwable, A> = result { this.bind() }
the general pattern is that the outer block (`either`/`result`) tells you the result type, and
bind()
"injects" an error into the context
r

raulraja

03/05/2024, 9:14 AM
raise.kt.cpp
f

Fred Friis

03/06/2024, 1:52 AM
what about filterOrElse and filterOrOther? they look like they should do roughly the same thing? https://github.com/arrow-kt/arrow/issues/1179