4 ms·
The simplest but still most versatile one is probably fmap (also knowns as <$>) which lets you use a function on whatever is inside the burrito: (+1) <$> Just
by unhammer 7y ago
The simplest but still most versatile one is probably fmap (also knowns as <$>) which lets you use a function on whatever is inside the burrito:
(+1) <$> Just 1 → Just 2
(+1) <$> getIntFromThatUserThatOnlyTypes1 → IO 2
(+1) <$> [1,2] → [2, 3]
((+1) <$> ask) 1 → 2
Want let a function eat several burritos without making a mess? Keep your burritos apart with <>:
take <$> Just 2 <> Just "abc" → Just "ab"
etc.
You can be quite productive without moving past the burrito analogy.
- lmm 7y agoNo. If you attempt to write monad-generic code using the burrito analogy you will inevitably write broken code, causing serious problems for other users and/or your future self. Burritos may be a good analogy for some subset of monads, e.g. collections, but they are not a good analogy for monads in general: the function you pass to fmap may be executed now or later, zero, one, or many times, before or after a function you pass to a later fmap.
- unhammer 7y agoNah, like any bad analogy, it can be stretched :) Once you've moved past the "lies-to-children[1]" version, you can ascend to the real meat of the burrito: https://blog.plover.com/prog/burritos.html https://blog.plover.com/prog/burritos.html And once you've fully grokked burritos in terms of monads, you can finally reach that zen level where you solve your problems without even a single line of code (because you've started working at the burrito shop instead: http://chrisdone.com/posts/monads-are-burritos http://chrisdone.com/posts/monads-are-burritos ). [1] https://en.wikipedia.org/wiki/Lie-to-children https://en.wikipedia.org/wiki/Lie-to-children
- unhammer 7y agotoo late to edit, but that should look like (+1) <$> Just 1 → Just 2 (+1) <$> getIntFromThatUserThatOnlyTypes1 → IO 2 (+1) <$> [1,2] → [2, 3] ((+1) <$> ask) 1 → 2 and take <$> Just 2 <*> Just "abc" → Just "ab" as if the operator line noise wasn't bad enough already :)