News for hackers. Stuff that matters.
Re:ClojureScript 101 by michaelsbradley on Tuesday November 12, 2013 @09:05PM (#6722870)
Jim Duey's blog post on the State Monad was helpful to me[1].

I agree that one shouldn't reach for monads just because one finds them interesting or neat, or challenging for that matter.

An analogy: consider that in many cases, `while` and `for` loops are great when looping is what's called for. But there are many times when recursion is more attractive still. But what to do if your language doesn't have TCO, and blowing the callstack is a real concern? Well, then you can use trampolines![2]

Now, trampolines are great, but you don't need to always reach for them. An analysis should be done of the problem at hand, and "the right tool chosen for the task", which may be a simple `while` loop. The same goes for monads. Are there cases where the State Monad makes the most sense among other programming patterns? Yes, I think so. Particularly, when you are trying to model a multi-step and nested-steps computation where the steps are intertwined in such a way that one can easily identify "global state" at work. In that case, the State Monad is a tool that will let you thread the global state through the steps and levels of computation without having to twist yourself into knots doing it some other way AND without relying on unfettered mutability.

[1] http://www.clojure.net/2012/02/10/State/

[2] http://raganwald.com/2013/03/28/trampolines-in-javascript.ht...

[&] http://clojure.github.io/clojure/clojure.core-api.html#cloju...

There are no comments on this comment yet.