A monad it a parametric type with two functions defined, $flatMap$ and $unit$.
Monads must satisfy the following three laws:
Associativity law: $$ m.flatMap(f).flatMap(g) = m.flatMap ( x => f(x).flatmap(g)) $$
Left unit law: $$ unit(x).flatMap(f) = f(x) $$
Right unit law: $$ m.flatMap(unit) = m $$
trait Monad[T] { def flatMap[U](f: T => Monad[U]): Monad[U] }
def unit[T](t: T): Monad[T]
Monads must satisfy the following three laws:
Associativity law: $$ m.flatMap(f).flatMap(g) = m.flatMap ( x => f(x).flatmap(g)) $$
Left unit law: $$ unit(x).flatMap(f) = f(x) $$
Right unit law: $$ m.flatMap(unit) = m $$
No comments:
Post a Comment