Skip to content

Instantly share code, notes, and snippets.

@ZhihaoLau
Last active June 1, 2016 14:32
Show Gist options
  • Save ZhihaoLau/19d1a913f5ee4ce6b7ce76ffa6c2d1d2 to your computer and use it in GitHub Desktop.
Save ZhihaoLau/19d1a913f5ee4ce6b7ce76ffa6c2d1d2 to your computer and use it in GitHub Desktop.
[ES6] Using generator with for ... of statement
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