Monads
In the preceding section, we defined functors. With their map
methods, the standard Scala collections seem to be good examples of functors. We should, however, emphasize again that a functor doesn't mean a collection—it can be a container and any custom-defined class. Based on an abstract map
method and the rules it follows, we can define other functions that will help us reduce code duplication. However, there are not many exciting things we can do based on a mapping only. In our programs, we will have different operations, some of which not only transform a collection or an object, but also modify it in some way.
Monads are another one of those scary terms that come from category theory, which we will try to explain in a way that you will be able to easily understand, identify, and use in your daily routine as a developer.
What is a monad?
We already talked about laws earlier in this chapter. The monoid is defined based on some laws it follows, and these laws allow us...