Skip to content

Instantly share code, notes, and snippets.

@vndmtrx
Created December 13, 2012 19:26
Show Gist options
  • Select an option

  • Save vndmtrx/4279022 to your computer and use it in GitHub Desktop.

Select an option

Save vndmtrx/4279022 to your computer and use it in GitHub Desktop.
FizzBuzz using Coroutines
#Decorador de Função
def coroutine(func):
def start(*args,**kwargs):
cr = func(*args,**kwargs)
next(cr)
return cr
return start
# Source
def source_fizzbuzz(fim, target):
i = 1;
while i <= fim:
target.send(i)
i += 1
target.close()
#Filter
@coroutine
def filter_fizzbuzz(target):
while True:
i = (yield)
if i % 15 == 0: target.send("FizzBuzz")
elif i % 3 == 0: target.send("Fizz")
elif i % 5 == 0: target.send("Buzz")
else: target.send(i)
i += 1
#Sync
@coroutine
def printer():
while True:
linha = (yield)
print(linha)
if __name__ == "__main__":
source_fizzbuzz(20, filter_fizzbuzz(printer()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment