5 ms·
Functional purity would be nice, but TCO not strictly necessary to be program very functionally. For instance, here TCO would be necessary to prevent the stack
by cdumler 14y ago
Functional purity would be nice, but TCO not strictly necessary to be program very functionally. For instance, here TCO would be necessary to prevent the stack from being over-run:
function fact(x) {
return (function factIter(i, total){
if(i<=1){ return total; }
return factIter(i-1, total*i);
})(x,1);
}
However, if you're willing to allow mutation in non-accessible code or closures, there are options:
function fact(x) {
if(i<=1) { return 1; }
return _.range(1,x+1).reduce(
function(sum, x) { return sum*x; }
);
}