Last Updated: February 25, 2016
·
1.341K
· 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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside