Skip to content

Instantly share code, notes, and snippets.

@virasak
Last active August 29, 2015 14:01
Show Gist options
  • Save virasak/b0acdd13c962323102c2 to your computer and use it in GitHub Desktop.
Save virasak/b0acdd13c962323102c2 to your computer and use it in GitHub Desktop.
Tail Call Optimization for JS
module.exports = function tailrec(f) {
var args = null;
return function () {
if (args) return (args = arguments);
var result = (args = arguments);
while (result === args) result = f.apply(this, args);
args = null;
return result;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment