Skip to content

Instantly share code, notes, and snippets.

@janneh
Created November 17, 2015 21:20
Show Gist options
  • Save janneh/9f772bc82d47ffe6c11a to your computer and use it in GitHub Desktop.
Save janneh/9f772bc82d47ffe6c11a to your computer and use it in GitHub Desktop.
Javascript generator function for infinite fibonacci
function* fibonacci() {
let x = 0
let y = 1
yield x
yield y
while (true) yield x < y ? x = x + y : y = x + y
}
const fib = fibonacci()
fib.next() // {value: 0, done: false}
fib.next() // {value: 1, done: false}
fib.next() // {value: 1, done: false}
fib.next() // {value: 2, done: false}
fib.next() // {value: 3, done: false}
fib.next() // {value: 5, done: false}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment