Skip to content

Instantly share code, notes, and snippets.

@we4tech
Created July 28, 2017 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save we4tech/3550092c7705e647b213ac8a312ccd73 to your computer and use it in GitHub Desktop.
Save we4tech/3550092c7705e647b213ac8a312ccd73 to your computer and use it in GitHub Desktop.
Use ES6 generator to generate infinite fibonacci series
function *calcFib () {
let n = 0
let fib = function(v) {
return v <= 1 ? v : fib(v - 1) + fib(v - 2)
}
while (true) yield fib(n++)
}
let generator = calcFib()
generator.next() // 0
generator.next() // 1
generator.next() // 1
generator.next() // ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment