3 ms·
It would help any recursively-defined code, parsers of some input format are often more naturally represented that way, for instance, where rewriting as an impe
by ISV_Damocles 4y ago
It would help any recursively-defined code, parsers of some input format are often more naturally represented that way, for instance, where rewriting as an imperative loop requires mixing of different logical states, eg, a JS JSON parser might determine if the current character is for an object, array, or leaf type, and then pass the input along to whichever that is, and if it was an array, for instance, would then pass back to that initial function one character over to determine what the first type of the first element of the array is. This is easy in a recursive style and would require all of this logic to be mixed in the same function with some state tracking variable to decide which branch to use in imperative style.
You can use trampolines[1] to get around this, but there's a performance penalty versus TCO and other recursive optimizations.
[1]: https://blog.logrocket.com/using-trampolines-to-manage-large-recursive-loops-in-javascript-d8c9db095ae3/ https://blog.logrocket.com/using-trampolines-to-manage-large...