Created
December 2, 2011 14:03
-
-
Save elazarl/1423344 to your computer and use it in GitHub Desktop.
coroutine equivalent of http://play.golang.org/p/lKBbS56f-n
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
#!/usr/bin/env python | |
# coroutine equivalent of http://play.golang.org/p/lKBbS56f-n | |
def generate(s,even,odd): | |
for i,c in enumerate(s): | |
if i%2 == 0: | |
even.send(c) | |
else: | |
odd.send(c) | |
def count(char): | |
def count_(char): | |
sum = 0 | |
while True: | |
c = yield | |
if c == None: | |
break | |
if c == char: | |
sum += 1 | |
yield sum | |
rv = count_(char) | |
rv.next() | |
return rv | |
def main(): | |
c1,c2 = count('e'),count('o') | |
generate("counting the number of some arbitrary values",c1,c2) | |
print("%d odd %s"%(c2.next(),'o')) | |
print("%d even %s"%(c1.next(),'e')) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment