Skip to content

Instantly share code, notes, and snippets.

@elazarl
Created December 2, 2011 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elazarl/1423344 to your computer and use it in GitHub Desktop.
Save elazarl/1423344 to your computer and use it in GitHub Desktop.
coroutine equivalent of http://play.golang.org/p/lKBbS56f-n
#!/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