Created
April 26, 2019 07:46
-
-
Save esneko/c9f2457ff97d0fa6cab0f38dcad06b3e to your computer and use it in GitHub Desktop.
Generators
This file contains 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] | |
while (true) { | |
[prev, curr] = [curr, prev + curr] | |
yield curr | |
} | |
} | |
for (let n of fibonacci()) { | |
console.log(n) | |
if (n >= 10) { | |
break | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment