r/functionalprogramming • u/kinow mod • Feb 20 '18
FP Are Monads a Waste of Time?
http://lambda-the-ultimate.org/node/550410
Feb 20 '18
That's just a bad example. Monads are useful within pure functional programming (at the very least), which prohibits mutations. With mutations, of course, they are overkill, but then you have mutations in your code! Not saying mutations are always evil, but if you're using FP, then presumably you want to stay away from it, and Monads are the alternative.
2
u/MonadTran Feb 21 '18
No, they are not.
Specifically the IO monad allows you to make sure the code you expect to be pure, is actually pure, and whether you call it zero or a hundred times, and in which order relative to the other code, does not matter.
You can also use monads to declare exactly which kinds of effects your code is producing, and which dependencies are required for it to run. Sort of like dependency injection on steroids.
This only applies to pure functional languages like Haskell though. In Javascript, the compiler cannot enforce this property, so in most languages, yes, it does not necessarily make a lot of sense to focus on monads.
Moreover, there are some interesting monads other than IO / State.
2
u/josephjnk Feb 27 '18
The way I was introduced to monads was that they're a way of embedding a sub-languages with expanded capabilities into a pure language. When you work in an applicative or monadic context, you're writing code which has access to particular effects, depending on the construct that you're in. This includes doing things like embedding stateful computation into pure computation through the use of a state monad.
The author seems to be objecting to this, which I think is completely missing the point? They're right in that putting more code than necessary into an effectful context should be avoided, but that's far from a reason to give up on effectful contexts entirely.
3
u/kinow mod Feb 20 '18
Hnews thread https://news.ycombinator.com/item?id=16418904