How it works...
The state transformer that we defined is a pretty powerful construct. It lets us use another monad in the context of State Monad. Implementation may look a little hard to get the first time. However, if you remember that all the actions are performed in the internal monad (whatever it might be). To do this, we took advantage of the do.. syntax for the monad (and that it is a Functor and an Applicative instance). All the internal monadic actions are modified from m a to m (a, s) and that is how we are able to achieve embedding of a monad inside a state transformer. All monad transformers follow a similar strategy.
In the mtl implementation of state transformer, you will see two implementations, lazy and strict. In the strict ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access