Last active
November 9, 2015 22:02
-
-
Save freality/02f17179dd94164da9eb to your computer and use it in GitHub Desktop.
Fibonacci sequence implemented as ES6 generator (works in Firefox console and Node v 0.12 w/harmony)
This file contains 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
function *fibonacci() { | |
var curr = 1 | |
, last = 0 | |
, x; | |
yield 1; | |
while (true) { | |
x = last + curr; | |
last = curr; | |
yield curr = x; | |
} | |
} | |
var fib = fibonacci(); | |
console.log(fib.next().value); //returns 1 | |
console.log(fib.next().value); //returns 1 | |
console.log(fib.next().value); //returns 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment