Created
May 12, 2016 13:37
-
-
Save blogscot/cce2a0913b6728a1d20b1a62fdf1f331 to your computer and use it in GitHub Desktop.
Comparing JS and Py 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 *foo(x) { | |
# var y = 2 * (yield (x + 1)); | |
# var z = yield (y / 3); | |
# return (x + y + z); | |
# } | |
# | |
# var it = foo( 5 ); | |
# | |
# // note: not sending anything into `next()` here | |
# console.log( it.next() ); // { value:6, done:false } | |
# console.log( it.next( 12 ) ); // { value:8, done:false } | |
# console.log( it.next( 13 ) ); // { value:42, done:true } | |
# Apparently JS ES6 generators work almost the same as Python | |
def foo(x): | |
y = 2 * (yield (x+1)) | |
z = yield (y / 3) | |
yield x + y + z # return in Python 3.3+ | |
it = foo(5) | |
print(it.next()) | |
print(it.send(12)) | |
print(it.send(13)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment