A monad is a type of programming paradigm, that is usually fulfilled by a
flatMap
or equivalent operation.
Laws
Info
Left-Identity Law:
Monad.of(x).flatMap(x -> f(x))
must be the same asf(x)
Right-Identity Law:monad.flatMap(x -> Monad.of(x))
must be the same asmonad
Associative Law:monad.flatMap(x -> f(x)).flatMap(x -> g(x))
must be the same asmonad.flatMap(x -> f(x).flatMap(y -> g(y)))
Functor
A\functor is another programming paradigm. As the map
operation can be implemented through a flatMap
, a monad is a functor.
return monad.flatMap(x -> Monad.of(mapper))
The map
operation needs to fulfill as such:
Info
- preserving identity:
functor.map(x -> x)
is the same asfunctor
- preserving composition:
functor.map(x -> f(x)).map(x -> g(x))
is the same asfunctor.map(x -> g(f(x))
.