Last active
June 1, 2016 14:32
-
-
Save ZhihaoLau/19d1a913f5ee4ce6b7ce76ffa6c2d1d2 to your computer and use it in GitHub Desktop.
[ES6] Using generator with for ... of statement
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* fibonacci() { | |
let [prev, curr] = [0, 1]; | |
// using deconstruct feature implement reducer, really cool... | |
for (;;) { | |
[prev, curr] = [curr, curr + prev]; | |
yield curr; | |
} | |
} | |
for (let n of fibonacci()) { | |
if (n > 1000) | |
break; | |
console.log(n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment