Last active
March 19, 2022 03:27
-
-
Save diegoulloao/ebc65d772dc4e2c39c5e440fad7b9f75 to your computer and use it in GitHub Desktop.
Fibonacci with generator functions
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
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... | |
const fibonacci = function* () { | |
let n1 = 0 | |
let n2 = 1 | |
yield n1 | |
yield n2 | |
while (true) { | |
const last = n2 + n1 | |
n1 = n2 | |
n2 = last | |
yield last | |
} | |
} | |
const generator = fibonacci() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() | |
generator.next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@buskerone acá está!