jean
10/04/2021, 6:41 AMEither<Error, x>
, I got it too work but the code is rather cumbersome and difficult for anyone else to figure out quickly. This is a simplified version, but it works the same way :
class A
class B
class C(
val val1: String,
val val2: String,
val val3: String,
val a: A,
val b: B,
)
fun getA(val1: String): Either<Exception, A> = Either.Right(A())
fun getB(val1: String): Either<Exception, B> = Either.Right(B())
fun getCs(): Either<Exception, List<CBuilder>> = Either.Right(listOf(CBuilder("val1", "val2", "val3")))
class CBuilder(
val val1: String,
val val2: String,
val val3: String,
)
class EitherTest {
@Test
fun buildC() {
getCs().flatMap { builders ->
builders.map { builder ->
getA(builder.val1).flatMap { a ->
getB(builder.val1).map { b ->
C(builder.val1, builder.val2, builder.val3, a, b)
}
}
}.sequenceEither()
}
}
}
Is there a way to simplify the two first map
in one operator, and is there a way to avoid consequent nesting of map
and flatmap
?simon.vergauwen
10/04/2021, 8:30 AMeither.eager { }
is what you would use here, or either { }
if you’re inside suspend
.
either.eager {
val builders = getCs().bind()
builders.map { builder ->
val a = getA(builder.val1)
val b = getB(builder.val1)
C(
builder.val1,
builder.val2,
builder.val3,
a,
b
)
}.sequenceEither().bind()
}
jean
10/04/2021, 3:29 PMbind()
after getA
and getB
? Otherwise they are still of type Either<Error, x>
jean
10/04/2021, 3:30 PMjean
10/04/2021, 3:34 PMeither.eager<Exception, List<C>>
for the compiler to be happy