3 ms·
Which for loops don't require a mutating index? (apart from the object property "for __ in __" iterator)
by Torn 13y ago
Which for loops don't require a mutating index? (apart from the object property "for __ in __" iterator)
- tieTYT 13y agoThose ones. But what I meant was that it is idiomatic to use for loops vs recursion.
- michaelsbradley 13y agoThat'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).
- mackwic 13y agoConsidering that recursive loop with bounds is a sort of for loop, recursive for.