The concept of a monad
In the previous section, we saw quite a few operations for a linked list. A few of them, namely map
and flatMap
, are a common theme in many objects in functional programming. They have a meaning outside of the list. The map
and flatMap
methods, and a method to construct a monad from a value are what make such a wrapper object a monad. A monad is a common design pattern that is followed in functional programming. It is a sort of container, something that stores objects of some other class. It can contain one object directly as we will see; it can contain multiple objects as we have seen in the case of a linked list, it can contain objects that are only going to be available in the future after calling some function, and so on. There is a formal definition of monad, and different languages name its methods differently. We will only consider the way Java defines the methods. A monad must have two methods, called map()
and flatMap()
. The map()
method accepts a lambda that...