Wednesday 7 October 2009

On Learning Scala

I wanted to learn Scala because I was excited about its paradigms: functional, oo and imperative. Then I ordered the book Programming in Scala and started reading. As I mentioned in my previous post, the book is a complete guide for the language regardless of the programming languages you already know.

Along with reading the book I recommend using the Scala interpreter (apt-get install scala). Small examples in the book are done with the interpreter. In addition to implementing the examples, try whatever you like in the interpreter in order to get a feeling for the language.

My next step in learning Scala was an example application with Scala's Actors. Implementing a small program on my own helped me to understand the language. I know, that's nothing new, the way of learning a programming language is always the same. But I think that the simple application of your choice should be based upon one of the language's specialities. I picked Scala's Actors. On my way through the code I tried to use different styles of Scala's control structures. The book could provide a solution almost every time I stumbled upon something. Of course I used the Scala API specification as well.

One thing I had to google:
As a for loop in Scala has a return value, I wanted to use it with the ++ method (add all) of the collection.

// add the resulting collection of the for loop to a map
// compile error: illegal start of simple expression
map ++ for {
...
} yield ...

I think that the compiler sees a false-positive there. It took me some time to find a solution. Adding surrounding parentheses to the for expression helps.

// parentheses saved the day
map ++ ( for {
...
} yield ... )

Taxes, a simple example with Scala's actors


You need Git http://git-scm.com/ and Maven http://maven.apache.org/

git clone http://github.com/rawyler/taxactors
cd taxactors
mvn install