Skip to content

Instantly share code, notes, and snippets.

@messa
Last active May 26, 2017 15:26
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 messa/4cae32fd7f47817b63d0937ce203a7c7 to your computer and use it in GitHub Desktop.
Save messa/4cae32fd7f47817b63d0937ce203a7c7 to your computer and use it in GitHub Desktop.
either = lambda a, b: lambda i: a(i) or b(i)
plus = lambda a, b: lambda i: a(i) + b(i)
div = lambda n: lambda i: i % n == 0
iff = lambda c, a, b: lambda i: a(i) if c(i) else b(i)
nothing = lambda x: ''
itself = lambda x: x
const = lambda x: lambda i: x
# some hints how it works :)
assert div(3)(0) == True
assert div(3)(1) == False
assert div(3)(2) == False
assert div(3)(3) == True
assert div(3)(4) == False
assert div(3)(5) == False
div2or3 = either(div(2), div(3))
assert div2or3(0) == True
assert div2or3(1) == False
assert div2or3(2) == True
assert div2or3(3) == True
assert div2or3(4) == True
assert div2or3(5) == False
# the fizzbuzz
fizz = iff(div(3), const('Fizz'), nothing)
buzz = iff(div(5), const('Buzz'), nothing)
fizzbuzz = either(plus(fizz, buzz), itself)
for i in range(1, 20):
print(fizzbuzz(i))
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment