Usually people share their knowledge about monads right after they get to understand them. I decided to share my experience with monads when a friend of mine started studying this topic. So here it is - my trip into monads.
In this blog post I’m not going to give you the exhaustive list of all monads, or explain the theory behind monads. I’m going to share with you the example that I used to develop intuition about monads.
Let’s solve the following problem. We are going to build a library for parsing different values from a string. For the sake of simplicity we are going to build two functions. One function parses a number delimited by whitespaces. The other function will be parsing a string delimited by whitespaces. I’m going to illustrate what monad is in Go. Our Go will be a slightly special flavor of Go. It will have genercis. However all the idioms that I demonstrate here are imported from Haskell.
In this exercise let’s imagine that Go is a purely functional language. You can’t use any imperative constructs in our version of Go. With one exception though. Consecutive assignments are allowed. Those are not imperative constructs. Also our Go will have two features: generics and tuples. As a result half of the code here won’t compile in Go. This code can be successfully rewritten in Java and compiled. But I decided to go with the modified Go instead of Java because of Go’s brevity.
Let’s declare the type of our first function. The function that parses a string. This function will be accepting an input string and returning the result and the remaining string.
1 2 3 4 5 6 7 8
ParseInt will look very similar. It can even use parseString except it will need to convert the result to int.
1 2 3 4 5
Now let’s write a function that is supposed to use our two functions. It will parse an integer and a string from the provided input string. It will also return the remaining string.
1 2 3 4 5
Now, people who were implementing it for the first time in Haskell noticed that there is a repetition in this code. We pass the remainder of the previous parse function and pass it into the next one. They decided to abstract this repetition into a separate data structure. Let’s declare a new structure.
Now let’s change the signature of our parse functions a bit:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7
At this point we need to define another interface that our Parser is going to implement:
Now let’s implement bind function for Parser. Pay attention. This is the most difficult part of the whole narration.
1 2 3 4 5 6
What it does it return a function that is a chain of two functions together. Function p and function f. Bind method just takes two functions and composes them into a bigger function.
The implementation of our example now turns into the following:
1 2 3 4 5 6
Here we encounter another method of that every monad should implement - lift. In Haskell this method is called return, but I decided not to use this name in order to avoid confusion with the built in return. Here is the implementation of lift.
1 2 3 4 5
Lift just wraps a value into a function.
What we implemented here is called state monad. For me it was the hardest monad to understand. I hope this blog post helps you in your understanding of monads.