On my road to understanding Functional programming, one of the biggest hurdle is understanding all the mathematical concepts that are associated with the paradigm. Some of these topics include category theory, functors, monads, and the list goes on. My issue is, I just want to write functional code as soon as possible; I rather have the abridged version! Well, now I finally do, so let’s understand monads today!
Monads Are Actually Simple
A monad is a container that can change the contents of the container; you also need to be able to place objects in the container and take them out. This is what a monad’s main functions are. In more complex terms, it allows you to map a value or set of values to another set of values of the same or of a different type. A good example is an array in most programming languages; you can call map to create a new set of values from the old values (without changing the old array). Here’s an example to illustrate those three principles in code.
With that said, let me provide one more example of a monad written in ReasonML.
You can see the implementation details very clearly. Lift places a value in the box,
Is That All A Monad Really Is?
Maybe not for the Mathematically inclined; I think for us developers, this simple definition works wonders. Given this understanding, you can start seeing the monads around you in your day to day life such as arrays and lists.
How Is This Going To Improve My Code?
Understanding what a tool is, allows you to use it more effectively. For example, you could model input from the user with a monad. Take the user input, map an operation upon it(removing possible characters that could crash your system or harm the computer), and then return the information out of the container(a cleaned user string). That’s a trivial example, so let’s take this a step further.
As we move toward more interactions between us and our devices, we create more data; data has to be filtered, mapped, flatMapped, and reduced in different ways to get something that’s usable for analytical data. More data is unstructured, meaning, we need a good pipeline for creating useful data from randomness; monads provide that implementation interface.
With that said, I hope this post helps you understand monads. Try to use these concepts in your code; it will make handling any form of data simpler in the long term.