Skip to content

Instantly share code, notes, and snippets.

@freality
Last active November 9, 2015 22:02
Show Gist options
  • Save freality/02f17179dd94164da9eb to your computer and use it in GitHub Desktop.
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)
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