Last active
August 29, 2015 14:01
-
-
Save virasak/b0acdd13c962323102c2 to your computer and use it in GitHub Desktop.
Tail Call Optimization for JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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