Created
October 25, 2019 00:15
-
-
Save mubeta06/77f11a5404275c018b5a6907cebe3f30 to your computer and use it in GitHub Desktop.
Fun with 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
"""A hack to play around with generator acknowledgement.""" | |
def extract(): | |
for i in xrange(10): | |
ack = yield i | |
print('acking %d' % ack) | |
def transform(ext): | |
ack = None | |
while True: | |
message = ext.send(ack) | |
ack = yield message**2 | |
def load(xfmr): | |
ack = None | |
while True: | |
try: | |
message = xfmr.send(ack) | |
ack = message | |
except StopIteration: | |
print('all finished') | |
break | |
def main(): | |
load(transform(extract())) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment