object Program {
// Tests parity of numbers.
def isEven(number: Int) = number % 2 == 0
def isOdd(number: Int) = !isEven(number)
def main(args: Array[String]): Unit = {
// Generate even numbers.
println(
"Filter even")
val evens = Seq.range(0, 10).filter(isEven(_))
println(evens)
// Generate odd numbers.
println(
"Filter odd")
val odds = Seq.range(0, 10).filter(isOdd(_))
println(odds)
// Another way to get odd numbers.
println(
"Filter not even")
val odds2 = Seq.range(0, 10).filterNot(isEven(_))
println(odds2)
}
}
Filter even
Vector(0, 2, 4, 6, 8)
Filter odd
Vector(1, 3, 5, 7, 9)
Filter not even
Vector(1, 3, 5, 7, 9)