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)tmg
07/10/2018, 3:13 PMpakoito
07/10/2018, 3:45 PMpakoito
07/10/2018, 3:46 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)
}
pakoito
07/10/2018, 3:47 PMpakoito
07/10/2018, 3:47 PMpakoito
07/10/2018, 3:48 PMinterface 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)
}
pakoito
07/10/2018, 3:48 PMpakoito
07/10/2018, 3:49 PMpakoito
07/10/2018, 3:50 PMfun <A,B> map(list: List<A>, f: (A) -> B): List<B> =
list.flatMap { a -> just(f(a)) }
pakoito
07/10/2018, 3:52 PMObservable
can be chained in a monadic way, you need extra language support to use the Monad abstraction effectivelypakoito
07/10/2018, 3:52 PMpakoito
07/10/2018, 3:52 PM