Last active
August 29, 2015 14:06
-
-
Save nicolaspayette/03722ed5f3f0dc1639f7 to your computer and use it in GitHub Desktop.
Scala FizzBuzz using Either
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
object FizzBuzz extends App { | |
def check[A](properties: (A ⇒ Boolean, String)*)(x: A): String = | |
properties | |
.foldLeft(Left(x.toString): Either[String, String]) { | |
case (acc, (p, s)) ⇒ | |
if (p(x)) Right(acc.fold(_ ⇒ s, _ + s)) | |
else acc | |
} | |
.merge | |
val fizzBuzz = | |
1 to 100 map check( | |
(_ % 3 == 0, "Fizz"), | |
(_ % 5 == 0, "Buzz")) | |
println(fizzBuzz.mkString(", ")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment