Gopal S Akshintala
02/27/2020, 2:10 PMEither.fx<L,R>{}
works with only one type of Either<L,R>
. Is there any way to work with different types of Either
to avoid nested fold?
val a: Either<Int, String> = "a".right()
val b: Either<String, Int> = 1.right()
Either.fx<String, Int> {
val bind = a.bind() // ^^^ Compiler Error
b.bind()
}
simon.vergauwen
02/27/2020, 2:19 PML
is where flatMap
works over.simon.vergauwen
02/27/2020, 2:20 PML
being present or in other words it can only return L
as a final result.simon.vergauwen
02/27/2020, 2:21 PMfun <A, B, C> EitherOf<A, B>.flatMap(f:(B) -> Either<A, C>): Either<A, C>
simon.vergauwen
02/27/2020, 2:21 PMa.flatMap { b }
which doesn’t compileGopal S Akshintala
02/27/2020, 3:13 PMGopal S Akshintala
02/27/2020, 3:18 PMval fold = a.fold(
{ it.toString() },
{
b.fold(
{ it },
{ it.toString() }
)
}
)
simon.vergauwen
02/27/2020, 3:46 PMmapLeft
https://arrow-kt.io/docs/apidocs/arrow-core-data/arrow.core/-either/map-left.htmlsimon.vergauwen
02/27/2020, 3:46 PMflatMapLeft
can still be implemented / contributed to Arrow Core 😉simon.vergauwen
02/27/2020, 3:47 PMsimon.vergauwen
02/27/2020, 3:47 PMEither.fx<String, Int> {
val bind = a.mapLeft { it.toString() }.bind()
b.bind()
}
Gopal S Akshintala
02/28/2020, 11:10 AM