Skip to content

Instantly share code, notes, and snippets.

@InPermutation
Last active December 14, 2015 12:38
Show Gist options
  • Select an option

  • Save InPermutation/5087433 to your computer and use it in GitHub Desktop.

Select an option

Save InPermutation/5087433 to your computer and use it in GitHub Desktop.
Using throw.js, show how to tail-call optimize a recursive function using ...exceptions?!
var throwup = require('throw').throwup, makecallable = require('throw').makecallable;
var recurse = throwup(_recurse);
var callable_recurse = makecallable(_recurse);
var result = callable_recurse(parseInt(process.argv[2], 10) || 5000, 0);
console.log('found', result);
function _recurse(n, j) {
if(n>0) return recurse(n-1, j+1);
else return j;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment