Last active
February 11, 2020 19:57
-
-
Save abdolence/27640bf373fe914b7504ce8408d7b1d6 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
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