Hi, is there a support in Arrow for transforming: ...
# arrow
d
Hi, is there a support in Arrow for transforming: •
() -> Either<Failure, Result>
and •
(Failure) -> Error
to
() -> Either<Error, Result
. All I want to do is to “decorate” a function returning
Either
with error mapping.
s
Hey @dnowak,
That is certainly possible, but you’ll probably want to adapt/lift those functions to fit a more general signature.
Either#redeemWith
takes: • (Failure) -> Either<Error, B> • (Result) -> Either<Error, B> So with that you can apply the desired transformations.
Copy code
val initial: () -> Either<Failure, Result> = TODO()
val error: (Failure) -> Error = TODO()
val success: () -> Either<Error, Result> = TODO()

val r: Either<Error, Result> =
  initial()
    .redeemWith({ fail: Failure ->
       error(fail).left()
    }, { _: Result ->
      success()
    })
d
Hi @simon.vergauwen, sorry for not being accurate. I just want to map the error in Either but have a as a result a function.
Copy code
val initlial: () -> Either<Failure, Result> = TODO()
val mapError: (Failure) -> Error = TODO()
val mapped: () -> Either<Error, Result> = {
    initial().mapLeft(::mapError)
}
And actually what I’m looking for would be an extension function to to apply error mapping to a function that returns
Either
Copy code
val mapped = initial.mapLeft(::mapError)
s
Ah, such functionality does not exist out of the box but you could achieve it by composing two different things inside Arrow Core.
Copy code
intial.andThen { it.mapLeft(::mapError) }
Where
andThen
is a function for composing functions with each other.
d
Thanks 🙂