kenkyee
07/10/2018, 1:11 PMrawtoast
07/10/2018, 1:28 PMmap
and flatmap
.
There are additional rules (Monad Laws, etc), but I wouldn’t look into that until you are comfortable with using existing monads.
You could see Java’s Optional
as a monad. It doesn’t strictly meet the criteria as it violates the Monad Laws, but as a beginner it may help to think of it as one.Andreas Sinz
07/10/2018, 1:37 PMtmg
07/10/2018, 3:11 PMflatMap
is what Monad gives you (besides some garantees if you respect the monad laws). map
comes from Funtors. (I guess you could call Monads a "subclass" of Functors)pakoito
07/10/2018, 3:45 PMinterface ObservableMonad {
fun <A> just(a: A): Observable<A> = Observable.just(a)
fun <A, B> flatMap(obs: Observable<A>, f: (A) -> Observable<B>): Observable <B> = obs.flatMap(f)
}
interface ListMonad {
fun <A> just(a: A): List<A> = listOf(a)
fun <A, B> flatMap(list: List<A>, f: (A) -> List<B>): List <B> = list.flatMap(f)
}
fun <A,B> map(list: List<A>, f: (A) -> B): List<B> =
list.flatMap { a -> just(f(a)) }
Observable
can be chained in a monadic way, you need extra language support to use the Monad abstraction effectively