Skip to content

Instantly share code, notes, and snippets.

@mubeta06
Created October 25, 2019 00:15
Show Gist options
  • Save mubeta06/77f11a5404275c018b5a6907cebe3f30 to your computer and use it in GitHub Desktop.
Save mubeta06/77f11a5404275c018b5a6907cebe3f30 to your computer and use it in GitHub Desktop.
Fun with Generators
"""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