Last active
August 29, 2015 14:14
-
-
Save PhiLhoSoft/8f78fd06f8306a4e0578 to your computer and use it in GitHub Desktop.
Ceylon functional FizzBuzz, two versions
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
shared void fizzBuzz() | |
{ | |
print("## FizzBuzz 1 ##"); | |
Iterable<String> fizzBuzz1(Integer count = 20) | |
{ | |
return (1 .. count).map((Integer c) => | |
15.divides(c) then "FizzBuzz" else | |
(3.divides(c) then "Fizz" else | |
(5.divides(c) then "Buzz" else c.string))); | |
} | |
for (p in fizzBuzz1(50).partition(5)) { print(p); } | |
print("## FizzBuzz 2 ##"); | |
Iterable<String> fizzBuzz2(Integer count = 20) | |
{ | |
return (1 .. count).map( | |
function (Integer c) | |
{ | |
value f = 3.divides(c); | |
value b = 5.divides(c); | |
return | |
"``f then "Fizz" else ""`` | |
``b then "Buzz" else ""`` | |
``!f && !b then c else ""``" | |
.replace("\n", ""); // Because I break down the string for readability | |
}); | |
} | |
for (p in fizzBuzz2(50).partition(5)) { print(p); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment