Created
December 13, 2012 19:26
-
-
Save vndmtrx/4279022 to your computer and use it in GitHub Desktop.
FizzBuzz using Coroutines
This file contains hidden or 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
| #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