Skip to content

Instantly share code, notes, and snippets.

@nicolaspayette
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolaspayette/03722ed5f3f0dc1639f7 to your computer and use it in GitHub Desktop.
Save nicolaspayette/03722ed5f3f0dc1639f7 to your computer and use it in GitHub Desktop.
Scala FizzBuzz using Either
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