3 ms·
Monads are useful even in imperative languages. They're just a context with a way of specifying how actions get chained together. Scala, Swift, and a number of
by actsasbuffoon 12y ago
Monads are useful even in imperative languages. They're just a context with a way of specifying how actions get chained together. Scala, Swift, and a number of other imperative languages have adopted monads (like Maybe) to great effect.
Conversely, the state monad (for example) has seen less adoption. Probably because it's a little difficult to grasp, and imperative languages just rely on mutable state for a similar effect. The state monad provides some nifty extras above and beyond variables as they appear in imperative languages, but you can't argue with the low learning curve of throwing a value into a variable and mutating it.
As for the article, I really like the API presented here. I'd worry about the performance penalty of sprinkling method_missing throughout my code, but the code clarity is phenomenal.
- techdebt5112 12y agoonly in languages with first class functions.
- dragonwriter 12y ago> Scala, Swift, and a number of other imperative languages have adopted monads (like Maybe) to great effect. Much of the use of "monads" in many other languages -- and particularly all the examples here -- really rely only on that subset of monad functionality that defines applicative functors. > Conversely, the state monad (for example) has seen less adoption. The use of the state monad really relies on it being a monad and not a mere applicative functor; I'm not sure if that's related, but I think that applicative functors are an easier thing to wrap your head around than monads.
- nshepperd 12y agoNo. #and_then here is monadic bind (>>=), and is necessary for all three use cases he described - nested nil checks, nested iteration, nested IO actions/callbacks.
- deleted 12y ago[deleted]
- theflubba 12y agoNo. Scala is a very functional language that you can write imperative code with. Swift is not functional and doesn't have real monads. Ruby monads are just a way of obfuscating your code. Monads are more than the monadic bind >>=. You cannot just use monads and ignore the rest of category theory, it's like using a stick to fix the engine of a car. Useless. Just use a real functional language so you don't have to deal with this tomfoolery. Example of a real monad: http://www.haskell.org/haskellwiki/Monad http://www.haskell.org/haskellwiki/Monad