In Javascript, ES5, the standard way to deal with stack overflow from "too
much recursion" is to convert the recursive function to its tail call
recursion form and then use trampoline or some other tco technique

Factorial not written for tail call optimization:

function factorial(n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n-1);
    }
}
The factorial function invokes * last, not factorial. This implementation
cannot be optimized.

Factorial written for tail call optimization:

function factorial(n) {
    function recur(n, acc) {
        if (n == 0) {
            return acc;
        } else {
            return recur(n-1, n*acc);
        }
    }
    return recur(n, 1);
}

running either busts the stack

factorial(56787)
Uncaught RangeError: Maximum call stack size exceeded

So far from what I read I think every loop in cljs is done via recursion
(or "recur" to be exact) so does cljs automatically converts a recursive
function to its tail call form and optimizes it via google closure (does
that have tco?) or converts to iterative JS?

Also, is their a branch of cljs that outputs ES6 code and maybe takes care
of tco?

More importantly, am i missing something bout how recur works in cljs?

This is just exploratory. I don't have a use case, but it's such a basic
thing to get out of the way.

Thanks,

Marc

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojurescript+unsubscr...@googlegroups.com.
To post to this group, send email to clojurescript@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to