Last active
May 26, 2017 15:26
-
-
Save messa/4cae32fd7f47817b63d0937ce203a7c7 to your computer and use it in GitHub Desktop.
This file contains 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
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)) |
This file contains 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
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