Created
May 2, 2014 06:47
-
-
Save bookest/db9a734d9b6512f86202 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
/** | |
* Port of the FizzBuzz solution described in | |
* [http://themonadreader.files.wordpress.com/2014/04/fizzbuzz.pdf | |
* FizzBuzz in Haskell by Embedding a Domain-Specific Language] to | |
* scala. | |
*/ | |
object FizzBuzz { | |
def fizzbuzz(n:Int):String = { | |
def test(d:Int, s:String)(x:String => String) = | |
if (n % d == 0) Function.const(s ++ x(""))_ | |
else x | |
(test(3, "fizz")_ compose test(5, "buzz")_)(identity[String])(n.toString) | |
} | |
def main(args: Array[String]) = 1 to 15 foreach (fizzbuzz _ andThen println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment