4 ms·
A monad is just a fancy name for a design pattern in functional programing that manages side effects so you can write what looks like imperative code but that d
by williamcotton 2y ago
A monad is just a fancy name for a design pattern in functional programing that manages side effects so you can write what looks like imperative code but that does some other stuff behind the scenes, eg, logging, async, exception handling.
They only make sense in a functional language that has support for “type wrappers”, eg, Result, Option, or custom made types like a ResultWriter.
The gist is you define a bind function that takes two arguments (a white lie), the first one of those “wrapped values” and the second a “function that takes an unwrapped value, does something, and returns a wrapped value”, and then the bind function “unwraps the value from the first arg and calls the second arg with that unwrapped value”.
This lets you chain together these “functions that take a value, does something, and then returns a wrapped value” and if you can define an infix operator it makes for for legible code that’s easy to read and modify. It looks even better in F#’s computation expression syntactic sugar.
Of course I am now going to try to explain this better with Yet Another Monad Tutorial to torture the internet with…
- recursive 2y agoI should have issued a warning against trying to educate me. But I also know that explaining monads is like irresistible crack for those in the know. I'm half-convinced I have a learning disability that specifically affects my ability to comprehend monads. Years ago, I gave it a serious attempt. At this point, I think I'll be satisfied to never quite get it. I totally understand Option, (I think?) but I completely fail to understand what that has to do with side effects. I'm ok with this. Thanks for the effort anyway. I don't understand what ResultWriter is or could be. I don't understand what the white lie is.
- williamcotton 2y agoThe white lie is that in functional programming a function only takes a single argument and uses currying to mimic multiple arguments. Like this in JS: let f = (x) => (y) => x * y; let p = f(2)(3); Is this in F#: let f x y = x * y let p = f 2 3 A ResultWriter is just a wrapper type that takes a tuple of a Result and a list of log messages. The side effects are you can write imperative code that works with normal values but does exception handling and log messaging behind the scenes and without it cluttering the task at hand. So you write some functions that return either Success wrapping a value with some good news log message or an Error with some bad news log message. Then with a bind function or especially with a computation expression in F# you can use those functions without dealing with the exceptions and log messages until after you’ve completed all of the steps. Unless you’re using a language that supports this kind of monad design pattern it won’t really make any sense!
- ashton314 2y agoWell, actually, it's really just like a burrito…