4 ms·
A monad is a box that can contain a value. You can run an operation on the value inside the box. An example of a monad is the Option monad: the box can either
by assbuttbuttass 4y ago
A monad is a box that can contain a value. You can run an operation on the value inside the box.
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.
- bluefirebrand 4y agoThis seems like the most clear explanation of what a monad is that I've ever seen. Thanks for this!
- badpun 4y agoI'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?
- Tainnor 4y agoYes. "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.