dflemstr
It's a ball.
Well, I see the benefits of python. Scala is at least as powerful as python, but because Python puts alot of stuff in syntax instead of libraries, it becomes easier to read (at least for those not used to the language).
Example: Here's a complete recursive quicksort-implementation ins cala for a list of ints:
CODE
def qsort(lst: List[Int]):List[Int] =
lst match {
case Nil => Nil
case pivot::tail => qsort(tail filter { _ < pivot })
::: pivot :: qsort(tail filter { _ >= pivot })
}
For those not accustomed to Scala's extremely capable pattern matching system (à la Erlang) and its very good string and list library will read that piece of code as if it were gibberish
Note that the only operators used in that piece of code are the "=>"-operator, which means "returns", and the "match"-operator, which is similar to Erlang's match. Everything else is functions.
However, Scala can be readable, too. Here's a Unit test in Scala:
CODE
class StackSpec extends Spec with ShouldMatchers {
describe("A Stack") {
it("should pop values in last-in-first-out order") {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should equal (2)
stack.pop() should equal (1)
}
it("should be empty when created") {
val stack = new Stack[Int]
stack should be ('empty)
}
}
}
Example: Here's a complete recursive quicksort-implementation ins cala for a list of ints:
CODE
def qsort(lst: List[Int]):List[Int] =
lst match {
case Nil => Nil
case pivot::tail => qsort(tail filter { _ < pivot })
::: pivot :: qsort(tail filter { _ >= pivot })
}
For those not accustomed to Scala's extremely capable pattern matching system (à la Erlang) and its very good string and list library will read that piece of code as if it were gibberish
Note that the only operators used in that piece of code are the "=>"-operator, which means "returns", and the "match"-operator, which is similar to Erlang's match. Everything else is functions.
However, Scala can be readable, too. Here's a Unit test in Scala:
CODE
class StackSpec extends Spec with ShouldMatchers {
describe("A Stack") {
it("should pop values in last-in-first-out order") {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should equal (2)
stack.pop() should equal (1)
}
it("should be empty when created") {
val stack = new Stack[Int]
stack should be ('empty)
}
}
}
Last edited by a moderator: