Skip to content
CSLearner edited this page Nov 12, 2021 · 6 revisions

Monad

Definition: It is a statically typed language with generic support. And we can also treat this guy as a wrapper which takes given values then allows us to do some operations on them.

When analyzing Monad's source code, we can say "wow it is a class or trait".

Two important laws of Monad

The one is Identity Law and another is Associative Law.

Identity Law.(Examples are from Lazy)

As we all know, "Identity" means "same" or "no change". Thus, we can treat "of()" method as a fake-identity, because it behaves like identity.

static <T extends Comparable<T>> Lazy<T> of(Supplier<T> supplier) {
    return new Lazy<T>(Optional.ofNullable(supplier).orElseThrow());
}

Then we will use of method to discuss two different kinds of Identity Law: Left and Right

Left Identity Law

Clone this wiki locally