1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

import scalaz . _ object Main { case class FizzBuzz ( elems : String * ) implicit object FizzBuzzMonoid extends Monoid [ FizzBuzz ] { override def zero = FizzBuzz ( ) override def append ( f1 : FizzBuzz , f2 : = > FizzBuzz ) : FizzBuzz = FizzBuzz ( f1 . elems ++ f2 . elems : _ * ) } implicit object FuzzBuzzShow extends Show [ FizzBuzz ] { override def shows ( f1 : FizzBuzz ) : String = f1 . elems . mkString ( "" ) } def interpreter ( name : String , cond : Int = > Boolean ) : Int = > FizzBuzz = i = > if ( cond ( i ) ) FizzBuzz ( name ) else FizzBuzzMonoid . zero val fizz = interpreter ( "fizz" , ( i : Int ) = > ( i % 3 ) == 0 ) val buzz = interpreter ( "buzz" , ( i : Int ) = > ( i % 5 ) == 0 ) def fizzBuzzer [ T ] ( interpreters : ( Int = > T ) * ) ( implicit m : Monoid [ T ] , s : Show [ T ] ) : Int = > String = i = > { val interpreted : T = interpreters . map ( _ . apply ( i ) ) . fold ( m . zero ) ( m . append ( _ , _ ) ) if ( interpreted == m . zero ) i . toString else s . shows ( interpreted ) } def main ( args : Array [ String ] ) { ( 1 to 100 ) . map ( fizzBuzzer ( fizz , buzz ) ) . foreach ( println ) } }