This is definitely interesting, and cool, but I'm not sure it's practical.
You might be trading stack space for heap space by using the trampoline. Closures, those returned functions, will require some kind of allocation and tracking. That is likely the dominant cost in the trampoline setup. I would thus argue that efficiency can't be the motivation.
If the motivation is clarity, I also tend to see the trampoline version as less clear than the original recursive form, and the loop form.
You are absolutely correct, closures can lead to memory leaks, specially in this type of situation. That's why it should be used carefully and only when necesary, being that as it may, I would have seriosly loved for TCO to have been implemented.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is definitely interesting, and cool, but I'm not sure it's practical.
You might be trading stack space for heap space by using the trampoline. Closures, those returned functions, will require some kind of allocation and tracking. That is likely the dominant cost in the trampoline setup. I would thus argue that efficiency can't be the motivation.
If the motivation is clarity, I also tend to see the trampoline version as less clear than the original recursive form, and the loop form.
You are absolutely correct, closures can lead to memory leaks, specially in this type of situation. That's why it should be used carefully and only when necesary, being that as it may, I would have seriosly loved for TCO to have been implemented.