Last Updated: February 25, 2016
·
1.032K
· rane

Using _.trampoline for recursive functions

http://runnable.com/U5xjrghR-NEC1pOa/_-trampoline-example-for-node-js

Based on http://raganwald.com/2013/03/28/trampolines-in-javascript.html

function factorial(n) {
  var _factorial = function myself(acc, n) {
    return n ? function() {
      return myself(acc * n, n - 1);
    } : function() {
      return _.done(acc);
    }
  }

  return _.trampoline(_factorial, 1, n);
}

factorial(32768)
// Infinity