Skip to content

Instantly share code, notes, and snippets.

@bookest
Created May 2, 2014 06:47
Show Gist options
  • Save bookest/db9a734d9b6512f86202 to your computer and use it in GitHub Desktop.
Save bookest/db9a734d9b6512f86202 to your computer and use it in GitHub Desktop.
/**
* 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