DEV Community

Discussion on: Do you even recurse?... And if you do, do you do it safely?

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

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.

Collapse
 
pichardoj profile image
J. Pichardo

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.