Re:Ask HN: ELI5 What Is a Monad?
by bluefirebrand on Monday November 14, 2022 @12:09PM
(#33597253)
This seems like the most clear explanation of what a monad is that I've ever seen. Thanks for this!
I'm not an FP expert by any means, but isn't having a flat-map operation (i.e. being able to combine two monads together, with result being another monad) a defining part of being a Monad?
Yes.
"A value in a box" is more like a metaphor for a functor.
The key part about monads is that you can "merge the boxes" (to strain the analogy). Or, as I'd prefer to think about it, to merge different effects.
An example of a monad is the Option monad: the box can either contain a value or it can be empty. The monad operation in this case modifies value in the box, or does nothing if the box is empty.
Another example is the IO monad, you can think of the box in this case as representing "the state of the world". The monad operation can modify the value in the box, but it can also write in sharpie on the outside of the box (perform IO)
In general a monad is any data structure where you have some boxed value and you can operate on the value without opening the box.