Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,7 +1,10 @@
|
|||
for (x <- 1 to 100) println(
|
||||
(x % 3, x % 5) match {
|
||||
object FizzBuzz extends App {
|
||||
1 to 100 foreach { n =>
|
||||
println((n % 3, n % 5) match {
|
||||
case (0, 0) => "FizzBuzz"
|
||||
case (0, _) => "Fizz"
|
||||
case (_, 0) => "Buzz"
|
||||
case _ => x
|
||||
case _ => n
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
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 replaceMultiples(x: Int, rs: (Int, String)*): Either[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)
|
||||
def fizzbuzz = replaceMultiples(_: Int, 3 -> "Fizz", 5 -> "Buzz") fold(_.toString, identity)
|
||||
|
||||
1 to 100 map fizzbuzz foreach println
|
||||
1 to 100 map fizzbuzz foreach println
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
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))))
|
||||
def f(n: Int, div: Int, met: String, notMet: String): String = if (n % div == 0) met else notMet
|
||||
for (i <- 1 to 100) println(f(i, 15, "FizzBuzz", f(i, 3, "Fizz", f(i, 5, "Buzz", i.toString))))
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
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))))
|
||||
for (n <- 1 to 100) println(List((15, "FizzBuzz"), (3, "Fizz"), (5, "Buzz")).find(t => n % t._1 == 0).getOrElse((0, n.toString))._2)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue