Created
May 29, 2013 13:40
-
-
Save markus1189/5670323 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
// An implementation of FizzBuzz using monoids | |
import scalaz._, Scalaz._ | |
def makeFizzBuzz[A: Monoid](ps: Iterable[Int => A]): Int => A = | |
(i: Int) => ps.toList.foldMap(_.apply(i)) | |
def moduloMatch[A:Monoid](m: Int)(v: A): Int => A = | |
(i: Int) => (i % m == 0) ?? v | |
val fizzbuzz = makeFizzBuzz { Seq( | |
moduloMatch(3)("Fizz"), | |
moduloMatch(5)("Buzz"), | |
moduloMatch(7)("Bazz") | |
)} | |
(1 to 100) foreach { i => println(s"$i => ${fizzbuzz(i)}") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment