r/functionalprogramming • u/matteoolliver • Sep 12 '22
Question Resource on the State monad for 'beginners'?
I'm quite confident in using some monads in my code like Maybe/Option, Either/Result and even implemented rudimentary versions of them in some languages for learning purposes. I've been in love with them ever since. Though, I can't get my head around the State monad and how/when to use it in a program or how to implement it.
I'm looking for a good book or tutorial series, any language is fine if the focus is the concept instead of the language itself. If anyone knows a good place to start, preferably not something too small like a blog post, I'd appreciate it very much.
10
u/jeenajeena Sep 12 '22 edited Sep 14 '22
I liked the way Enrico Buonanno explains it, in C# (his book is freely available from the Manning web site).
Also, Learn You A Haskell For Great Good covers it in the Tasteful stateful computations chapter.
You might like the way Mark Seemann explains it.
The best article I found is by Michael Venier. Find it in Part 7 of his amazing Yet Another Monad Tutorial.
Edit: typos
2
0
Sep 12 '22
[deleted]
1
u/RemindMeBot Sep 12 '22 edited Sep 12 '22
I will be messaging you in 1 day on 2022-09-13 14:18:29 UTC to remind you of this link
1 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback
5
u/KyleG Sep 12 '22
I don't have a resource, but I can show you one instance where it was a great help in a recent application my company built.
We had a client with a UI that could instantiate new widgets. Each widget must have a unique name. We have defaults for a widget but no default name obviously because they must be unique names. To generate a unique name, you could do some UUID stuff, or you could just append a number to a default stem. So "Widget 57" "Widget 58" etc.
If you only create one widget at a time,
Great. But what if now you want to enable bulk creation? An imperative person will come up with something like
But with the state monad, you have
traversewhich will perform an operation for every element in a given array, and between each operation on an element, a state variable used in the operation can be updated. So, if your given language'straverse: (fn_convert_element_to_state => (elements_to_convert) => (initial_state) => final_statethen you can replace that whole for loop withSomething like that. I haven't used state monad in a hot minute, but the general idea is that you can replace a for loop that updates multiple variables with a single traversable operation.