So in Rxjava terms, a Monad is the lambda inside t...
# functional
k
So in Rxjava terms, a Monad is the lambda inside the .map operator?
r
No RxJava experience, but: The basic way to think about this is: A Monad is an object that holds a another object and provides two methods:
map
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.
a
@kenkyee its the other way around. Rx itself is the Framework (Monad) that the lambdas are executed in
😮 1
t
flatMap
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)
You can think of functors and monads as context or containers. But it's best if you know the math behind it
p
Monad is an object you can create that describes the way RxJava’s Observables can be chained
Copy code
interface 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)
}
and you can pass that as a parameter through your program, describing how to chain Observables
you can create them for lists and such
Copy code
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)
}
so you can abstract over them, and have special syntax
or create whole programs that are polymorphic to these types and can be run using lists or observables, or option, and always behave the same because you only have those two operations…and any other operation derived from it
Copy code
fun <A,B> map(list: List<A>, f: (A) -> B): List<B> =
  list.flatMap { a -> just(f(a)) }
so while
Observable
can be chained in a monadic way, you need extra language support to use the Monad abstraction effectively
for example, with async/await notation, what the article does as let!