I think the code snippet should be simple and don’t need to show too many features in Scala.
I find some good code examples in old Scala home page.
Some code snippets (came from here and there and modified) I like:
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
object Fib {
def fibonacci(n: Int): Int = n match {
case 0 | 1 => n
case _ => fibonacci(n - 1) + fibonacci(n - 2)
}
def main(args: Array[String]) {
val numbers = List(4, 2)
for (n <- numbers) println(fibonacci(n))
}
}
object Sort {
def quickSort(list: List[Int]): List[Int] = list match {
case Nil => Nil
case head :: tail =>
val (low, high) = tail.partition(_ < head)
quickSort(low) ::: head :: quickSort(high)
}
def main(args: Array[String]) {
val numbers = List(6, 2, 8, 5, 1)
println(quickSort(numbers))
}
}
The third one looks good, but I still think it is a bit complex. I try to use if/else instead of pattern match (Nil is odd for beginners) but still like the latter.