Re:Functional JavaScript: Closure
by tieTYT on Tuesday June 18, 2013 @03:21PM
(#5901375)
Those ones. But what I meant was that it is idiomatic to use for loops vs recursion.
[ Parent
| on HN ]
Re:Functional JavaScript: Closure
by michaelsbradley on Tuesday June 18, 2013 @04:17PM
(#5901771)
That's true, and may be reinforced somewhat by the fact that JavaScript doesn't eliminiate tail calls. In other words, there are situations where a recursive algorithm would be clean and compact, but might result in the call stack being blown inadvertently.
I ran into this recently while building a "practical" monads library for JavaScript. Each monad and transformer has sync and async variants. The sync variants can't use straight recursion because "deep" monadic binds will result in a blown call stack. The solution was/is to allocate on the heap in terms of an array that acts as a manually operated call stack (I think I essentially implemented a "trampoline" but I'm not 100% certain).