Skip to content

Instantly share code, notes, and snippets.

@royhowie
Last active August 29, 2015 14:24
Show Gist options
  • Save royhowie/fd63fd711f3a95f464ce to your computer and use it in GitHub Desktop.
Save royhowie/fd63fd711f3a95f464ce to your computer and use it in GitHub Desktop.
fibonacci sequence with generators
function* fib () {
var a = 0, b = 1, c;
yield 0
yield 1
while (true) {
c = b + a
a = b
b = c
yield c
}
}
var f = fib()
for (let i = 0; i < 7; i++) {
console.log(f.next().value)
// 0, 1, 1, 2, 3, 5, 8 ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment