In my project I've defined the following extensions to avoid having to declare the id monad on Reader map and flatmap.
Copy code
/**
* Extension function that fixes `Reader` so that you can `map` without needing to pass
* `arrow.core.Id.monad()` explicitly.
*/
fun <D, A, B> Reader<D, A>.map(f: (A) -> B): Reader<D, B> {
return this.map(arrow.core.Id.monad()) { f(it) }
}
/**
* Extension function that fixes `Reader` so that you can `flatMap` without needing to pass
* `arrow.core.Id.monad()` explicitly.
*/
fun <D, A, B> Reader<D, A>.flatMap(f: (A) -> KleisliOf<D, ForId, B>): Reader<D, B> {
return this.flatMap(arrow.core.Id.monad()) { f(it) }
}
Is there any blocker for actually just making Reader work this way in the lib? Is there any other monad instance that is valid for Reader?
s
simon.vergauwen
06/20/2020, 10:00 AM
Yes, you can use
Reader
with
IO
to combine it with effects, or any other
Monad
. It’s often composed together with
EitherT
and
IO
as well.
To describe a program that has a dependency, and runs effect-fully with a domain error or success value as result.
These APIs should already be available as extensions over