Last active
February 8, 2017 10:28
-
-
Save abiodun0/681c672525e5fb82981c5eb6f99c8339 to your computer and use it in GitHub Desktop.
Generators in python and javascript
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* getFibonacci() { | |
yield a = 0; | |
b = 1; | |
while (true) { | |
yield b; | |
b = a + b; | |
a = b - a; | |
} | |
} | |
for (num of getFibonacci()) { | |
if (num > 100) { | |
break; | |
} | |
console.log(num); | |
} |
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
def getFibonacci(): | |
yield 0 | |
a, b = 0, 1 | |
while True: | |
yield b | |
b = a + b | |
a = b - a | |
for num in getFibonacci(): | |
if num > 100: | |
break | |
print(num) |
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* getFibonacci(n) { | |
yield a = 0; | |
b = 1; | |
while (b < n) { | |
yield b; | |
b = a + b; | |
a = b - a; | |
} | |
} | |
console.log([...getFibonacci(100)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment