On the other hand, Monad is HARD to explain:
As a formal definition of a structure, Monad is
basically a group of types that can implement
unit/flatMap function that match the signature in
def unit[A](a: => A): F[A]
def flatMap[A,B](ma: F[A])(f: A => F[B]): F[B]
But that is not enough, we also have law that
Monad type need to fulfill so that we know our
implementation of unit/flatMap is correct.
// Monad Law
// left identity: f(a) == flatmap(unit(a), f)
// right identity: a == flatMap(a, x => unit(x))
// associativity: flatMap(a, x => flatMap(f(x), g)) == flatMap(flatMap(a, f)
, g)
As the name suggest, associative law means flatMap
operation obey the associative law similar to plus/multiple
(a+b) + c = a + (b+c)
As the name suggest, identity laws basically means we
have a unit function that server as a identity in monad, like
0 in addition, which similar to x + 0 = x, and 0 + x = x