Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Last active February 8, 2017 10:28
Show Gist options
  • Save abiodun0/681c672525e5fb82981c5eb6f99c8339 to your computer and use it in GitHub Desktop.
Save abiodun0/681c672525e5fb82981c5eb6f99c8339 to your computer and use it in GitHub Desktop.
Generators in python and javascript
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);
}
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)
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