ibcoleman
10/23/2021, 10:49 PMObservable<Either<T, U>>
and though the either {}
stuff is pretty straightforward, the only documentation I can find on nested monads is the old EitherT, OptionT documentation. Here Observable
is from rxjava2. What if I wanted to use a Reader and had to deal with Reader<Observable<Either<MyError, Aggregate
? Just very confused…
@Test
fun checkCallsWithEitherComprehensionTest() = runBlocking {
fun together(asset: Asset, agg: Aggregate): Either<MyError, AssetWithAggregate> =
Right(AssetWithAggregate(assetName = asset.name, aggName = agg.name, id = asset.id))
fun getAggregate(): Observable<Either<MyError, Aggregate>> =
Observable.just(Right(Aggregate(id="000", name="An Aggregate")))
fun getAsset(id: String): Observable<Either<MyError, Asset>> =
Observable.just(Right(Asset(id=id, name="An Asset")))
// How do I get rid of blockingSingle() to defer this stuff?
val togetherE: Either<MyError, AssetWithAggregate> = either {
val theAgg = getAggregate().blockingSingle().bind()
val theAsset = getAsset(theAgg.id).blockingSingle().bind()
together(theAsset, theAgg).bind()
}
// Check the values of the returned Either
togetherE.fold(
{_ -> assertTrue(false, "Error: Should be right!")},
{
assertEquals("An Asset", it.assetName)
assertEquals("An Aggregate" , it.aggName)
assertEquals("000", it.id)
}
)
}
raulraja
10/24/2021, 10:10 AMraulraja
10/24/2021, 10:11 AMraulraja
10/24/2021, 10:20 AMraulraja
10/24/2021, 10:31 AMibcoleman
10/24/2021, 11:48 AMsimon.vergauwen
10/26/2021, 10:22 AMsuspend
. An example of doing MTL like effects, using suspend
and Kotlin features can be found here:
https://github.com/nomisRev/KotlinEffectsPlayground/blob/main/src/commonMain/kotlin/example.kt
Currently still requires a hack, but soon it’ll be fully supported automatically by the compiler.
This allows you to avoid monad stacks completely, and just weave the context of your monad through your programs.
Basically requiring a single allocation per monad, for an infinite amount of binds.ibcoleman
10/27/2021, 9:39 PM