Created
March 29, 2015 04:14
-
-
Save teppeis/35eccdc8660b51b5ed53 to your computer and use it in GitHub Desktop.
Fibonacci with TCO https://google.github.io/traceur-compiler/demo/repl.html
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
// Options: --proper-tail-calls | |
"use strict"; | |
var cache = {}; | |
function fib(n) { | |
return fib_(n, _ => _); | |
} | |
function fib_(n, callback) { | |
if (n === 0) return callback(0); | |
if (n === 1) return callback(1); | |
if (cache[n]) return callback(cache[n]); | |
return fib_(n - 1, p1 => fib_(n - 2, p2 => callback(cache[n] = p1 + p2))); | |
} | |
console.log(fib(10000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment