Skip to content

Instantly share code, notes, and snippets.

@markus1189
Created May 29, 2013 13:40
Show Gist options
  • Save markus1189/5670323 to your computer and use it in GitHub Desktop.
Save markus1189/5670323 to your computer and use it in GitHub Desktop.
// 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