Monads for Developers with a Background in OO
Monads for Developers with a Background in OO
This article will try to explain what Monads are from an OO perspective.
More specifically how Monads solves a class of problems typically
solved differently in OO. It assumes
that you are reasonably well-versed in Java 8 features such as
Optional and Streams.
Monads as an Abstraction
As you probably know, FP puts more emphasis than OO on immutable state and
pure functions. In OO we usually solve “secondary concerns” like logging and metrics by
using side-effects. A member function might write to stdout directly, or
use some sort of mockable “Logger” interface, which we can use to inject
mocks to verify correct behaviour:
public static int impureAdd(int i, int j) {
System.out.printf("Adding %s and %s", i, j);
return i + j;
}
In FP, this is not considered good design. Instead, you are encouraged to
have your functions...