Skip to content

Instantly share code, notes, and snippets.

@dunossauro
Last active May 25, 2018 02:45
Show Gist options
  • Save dunossauro/b5a6631310cfa87c5129cb9a2d4e938b to your computer and use it in GitHub Desktop.
Save dunossauro/b5a6631310cfa87c5129cb9a2d4e938b to your computer and use it in GitHub Desktop.
"""
Minha contribuição ao buzzfizz funcional.
https://www.facebook.com/groups/python.brasil/permalink/1411831415588391/
"""
from functools import partial
from operator import mod, eq
def pipeline(*funcs):
def inner(arg):
start = arg
for func in funcs:
start = func(start)
return start
return inner
def isint(func: callable) -> callable:
def inner(n: int) -> int:
return func(n) if isinstance(n, int) else n
return inner
@isint
def fizz(n: int) -> [int, str]:
return 'fizz' if eq(mod(n, 3), 0) else n
@isint
def buzz(n: int) -> [int, str]:
return 'buzz' if eq(mod(n, 5), 0) else n
@isint
def fizz_buzz(n: int) -> [int, str]:
return 'fizzbuzz' if eq(mod(n, 5), 0) and eq(mod(n, 3), 0) else n
problem = pipeline(fizz_buzz, fizz, buzz)
apply = pipeline(partial(map, problem), list)
print(apply([1, 2, 3, 4, 5, 6, 7, 30])) # [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 'fizzbuzz']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment