max.cruz
05/18/2020, 1:56 PMsuspend fun init0(): Either<Failed, Loaded>
suspend fun init1(): Either<Failed, Loaded>
suspend fun init2(): Either<Failed, Loaded>
A group of independent suspend functions that return `Either<Failed, Loaded>`; those functions were previously composed with using Try.flatMap and to be able to migrate them to Either I added temporarily a trapdoor function like this:
inline fun <A, B, C> EitherOf<A, B>.flatMapInline(f: (B) -> Either<A, C>): Either<A, C> =
fix().let {
when (it) {
is Either.Right -> f(it.b)
is Either.Left -> it
}
}
val bootDeprecated = init0()
.flatMapInline { init1() }
.flatMapInline { init2() }
I want to move forward the approach, get rid of my flatMapInline trapdoor, and start using IO to deffer the effects. But my problem is that I don't figure out how to get the same behavior in a more expressive way. This was my best attempt:
val boot: IO<Either<Failed, Loaded>> = IO.fx {
!effect { init0() }
.effectMap { if (it.isRight()) init1() else it }
.effectMap { if (it.isRight()) init2() else it }
}
This works (from a pragmatic perspective) and I achieve the same short-circuiting the effect if one of them fails. However, I'm wondering if there is a better built-in way to express this. Thanks so much for any advice or recommendation and sorry in advance for the long post