Skip to content

Instantly share code, notes, and snippets.

@tokdaniel
Created April 8, 2021 11:37
Show Gist options
  • Save tokdaniel/d4950be033ffd81054014fe3502304f2 to your computer and use it in GitHub Desktop.
Save tokdaniel/d4950be033ffd81054014fe3502304f2 to your computer and use it in GitHub Desktop.
from functools import reduce
def compose2(f, g):
return lambda *a, **kw: f(g(*a, **kw))
def compose(*fs):
return reduce(compose2, fs)
def safeMod(input, divider):
return input % divider if type(input) == int else input
def is_divisible_by(divisor):
return (lambda replacer: (lambda value: replacer if safeMod(value, divisor) == 0 else value))
fizz = is_divisible_by(3)("Fizz")
buzz = is_divisible_by(5)("Buzz")
fizz_buzz = is_divisible_by(15)("FizzBuzz")
fizz_buzzer = compose(fizz, buzz, fizz_buzz)
result = map(fizz_buzzer, range(1, 100))
for i in result:
print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment