Skip to content

Instantly share code, notes, and snippets.

@abdolence
Last active February 11, 2020 19:57
Show Gist options
  • Save abdolence/27640bf373fe914b7504ce8408d7b1d6 to your computer and use it in GitHub Desktop.
Save abdolence/27640bf373fe914b7504ce8408d7b1d6 to your computer and use it in GitHub Desktop.
case class FizzBuzzRuleList[A]( rules: FizzBuzzRule[A]* ) {
def map( x: A ): String =
rules
.find( _.pred( x ) )
.map( _.value.apply() )
.getOrElse( x.toString )
}
object FizzBuzzRuleList {
def rule[A]( pred: A => Boolean, value: => String ): FizzBuzzRule[A] =
FizzBuzzRule[A]( pred, () => value )
}
import FizzBuzzRuleList._
val fizzBuzzRules = FizzBuzzRuleList[Int](
rule( x => x % 3 == 0 && x % 5 == 0, "FizzBuzz" ),
rule( _ % 3 == 0, "Fizz" ),
rule( _ % 5 == 0, "Buzz" )
)
val N = 100
fizzbuzz( N )( fizzBuzzRules.map ).foreach( println )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment