Hi all! I’m coming back to kotlin and arrow after ...
# arrow
i
Hi all! I’m coming back to kotlin and arrow after a bit of time away, and I’m having a little difficulty putting the pieces together--especially given the huge changes from 0.11 -> 1.0.0. I’m trying to compose functions that return
Observable<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…
Copy code
@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)
            }
        )
    }
r
At the moment we are constrained to ext functions with a single receiver but soon we will have multiple context receivers and these stacks of transformers are just suspend contexts. Reader in particular is natively supported by the language through ext functions. Placing an Observable inside or outside Either can be even later deferred if your function is suspend and returns the concrete type but contains the Observable and Either context in the list with multiple receivers. In the meantime it can be emulated with a single receivers and ext functions. Also flatMap over most datatypes (not Observable because multishot) can be turned into an effect block like the Either one. Here is an example https://kotlinlang.slack.com/archives/C5UPMM0A0/p1631326397023700
If you have a use case or small example program where you use Reader we can help rewrite the example to what we think it looks today with Arrow. 🙂
i
Thanks @raulraja, for the links and the library. I’ve been hoping to get back to my co’s Kotlin project so I can start to slowly integrate arrow, but my (limited) fp chops are all scalaz-style stuff but it looks like arrow’s mutated quite a bit form 0.nn! 😀
s
Yes, it deviated from the Haskell/Scala style of doing FP more in a direction that makes more sense to Kotlin. We’ve discovered a lot of new patterns using
suspend
. 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.
👀 1
☝️ 1
👍 1
i
Thanks @simon.vergauwen! I’ll definitely take a look…