Created
July 28, 2017 21:32
-
-
Save we4tech/3550092c7705e647b213ac8a312ccd73 to your computer and use it in GitHub Desktop.
Use ES6 generator to generate infinite fibonacci series
This file contains hidden or 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 *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