This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -1,6 +1,7 @@
(1 to 100) foreach {
case x if (x % 15 == 0) => println("FizzBuzz")
case x if (x % 3 == 0) => println("Fizz")
case x if (x % 5 == 0) => println("Buzz")
case x => println(x)
}
for (x <- 1 to 100) println(
(x % 3, x % 5) match {
case (0, 0) => "FizzBuzz"
case (0, _) => "Fizz"
case (_, 0) => "Buzz"
case _ => x
})

View file

@ -1,6 +1,8 @@
(1 to 100) map ( x => (x % 3, x % 5) match{
case (0,0) => "FizzBuzz"
case (0,_) => "Fizz"
case (_,0) => "Buzz"
case _ => x
}) foreach println
def replaceMultiples(x: Int, rs: (Int, String)*) =
rs map { case (n, s) => Either cond (x % n == 0, s, x) } reduceLeft ((a, b) =>
a fold ((_ => b), (s => b fold ((_ => a), (t => Right(s + t))))))
def fizzbuzz(n: Int) =
replaceMultiples(n, 3 -> "Fizz", 5 -> "Buzz") fold ((_ toString), identity)
1 to 100 map fizzbuzz foreach println

View file

@ -1,8 +1,2 @@
def replaceMultiples(x: Int, rs: (Int, String)*) =
rs map {case (n, s) => Either cond (x % n == 0, s, x)} reduceLeft ((a, b) =>
a fold ((_ => b), (s => b fold ((_ => a), (t => Right(s + t))))))
def fizzbuzz(n: Int) =
replaceMultiples(n, 3 -> "Fizz", 5 -> "Buzz") fold ((_ toString), identity)
1 to 100 map fizzbuzz foreach println
def f(a: Int, b: Int, c: String, d: String): String = if (a % b == 0) c else d
for (i <- 1 to 100) println(f(i, 15, "FizzBuzz", f(i, 3, "Fizz", f(i, 5, "Buzz", i.toString))))