Starting out with Scala

03 Jul, 2009
Xebia Background Header Wave

Scala has become more and more popular over the recent months/years. Its hybrid nature of being an imperative as well as functional language attracts a crowd from the Java world as well as functional fundamentalists coming from the world where statements like x=x+1 are looked at with the utter disbelief. It has been stated that Scala is ‘Java as it should have been’, but there are also numerous complaints about the language and its features (like not being side effect free, overly complex, too much of everything, too much abstraction, having a weird syntax, etc). The latter might actually be a proof of its popularity, since people seem to be actually using the language instead of just looking at it briefly and stopping, tired but happy, after having written hello world with it.
In this blog post, I’ll give you some (hopefully) useful tips how to best start if you want to learn this language, which is one of the candidates become ‘our next big language’ and surpass Java in this respect.

Pick an IDE, or not
The first problem you might encounter is that the various Scala IDE plugins have not yet reached the maturity of the Java support that you might have been accustomed to. Lots of hard work is in progress on this, but you still might experience hickups and freezes (as I have) of your favorite IDE. At this moment, the Netbeans plugin seems to be the most stable (this is the IDE that David Pollak, creator of Lift, seems to use). If you’re an eclipse user, you’ll probably want the nightly build version of the Scala plugin, since that’s the only one actively under development. This means that you need the Scala trunk to work with, however.
There’s also plenty of support for non-IDE’s, and recently I’ve tried switching back to emacs again. For some, this brings back memories to good old university days, others may find it the utmost horror. I must say, I found it to be a rather soothing experience after years of development using Eclipse. You’ll need to do some setup work to get a good Scala experience. An excellent blog post describing how to setup your emacs environment for Scala using Yasnippet, exuberant ctags and maven can be found here.
Try out the trunk
The latest released version of Scala is 2.7.5. The next version will be 2.8.0, but its release date is still not known. However, the invaluable Paul Philips has done many bug fixes and improvements in the Scala compiler and the REPL, all hanging out in the trunk. To take full advantage of this, check out the sources and build them. Note that the Scala collections API has been redesigned in the trunk, and differs from the 2.7.5 release. To get a good understanding of the new design, and the API in general, you’re encouraged to read the new collection SIP (Scala improvement proposal).
The standard work is Programming in Scala, co-authored by Martin Odersky (creator of Scala) himself. This book is lengthy but very thorough, providing plenty of examples. Another book that has seen the light is Beginning Scala by David Pollak (already mentioned). I have not read this, but reviews suggest this is also an excellent start. More books will hit the market this year.
Check out Lift
Eventually it is possible that you might start to really like Scala and want to use it in the enterprise. Lift is the first web framework written in Scala, created by David Pollak. Downloading and creating a simple web application to work is a no-brainer. The distribution comes with plenty of maven archetypes to get a basic web application ready in under than five minutes. Lift has its own database mapping framework, but there’s JPA support as well, if you like this. JTA integration is also being worked on.
Read some articles
There are loads of (both academic and slightly less academic) articles about Scala. For example, if you want to know the rationale behind Scala’s actor library design, read the scala actor papers.
Martin Odersky’s homepage also contains a long list of his publications.
It provides lots of material to provide insight in the design of Scala and its libraries. If you’re not that academically inclined and from a Java background, try The busy java developer’s guide to Scala, by Ted Neward. Provides a nice introduction.
Read some blogs.
Plenty of blogs available, here are some I like:

  • Daniel Spiewak Lots of useful Scala examples.
  • Tony morris. He’s the creator of functional java and of Scalaz. Beware however, if you check this out you might end up in deep functional waters where Monads reign.
  • Jonas BonerHe has lots of experience of using Scala in the real world, some of which he has blogged extensively about. Well worth the read.
  • Planet Scala Perhaps you don’t even need more than this. This aggregates numerous blog posts (including the one mentioned above) about Scala.

Join IRC
Scala has an IRC channel, which can be found here. Whenever you’re stuck at some problem you’re working at, there’s always the mailing lists, but many knowledgeable people hang out on a daily basis on the IRC channel. Use lodgeit to show the code you’re stuck with, and you’ll get an answer in no time.
Start coding.
Lift might be the choice if you really want to write some useful, practical real world web applications. If you’re totally impractical like me, pick a few problems from project euler, or the 99 problems projects (solutions in Scala can be found here. Many excellent programmers have taken this path, it’s fun and an excellent way to play around with the core Scala API.
To put into practice what I preach here, a few lines of Scala code to either whet your appetite or make you run away screaming. Note that my Scala knowledge is still in the pre-kindergarten stage, I’m barely able to speak, so laugh at will when viewing this code.
First, a randomly picked Euler problem, number 48, just because I like onliners:
(1 to 1000).map(x => new java.math.BigDecimal(x).pow(x)).reduceLeft((a,b) => a.add(b)).remainder(new java.math.BigDecimal(10).pow(10))
This one liner is not nearly as concise and neat as the Haskell version, but it will do. If nothing else, it has at least has the tiny merit of showing Scala – Java interoperability.
Another randomly picked and slightly more complex one, problem number 21:
object Euler21 {
def sumOfDivisors(number: Int): Int = {
List.range(1, number).filter{i => (number % i) == 0}.foldLeft(0){(a,b) => a+b}
def solver(): Int = {
(for (i <- 0 until 10000; di = sumOfDivisors(i); if(di > i && sumOfDivisors(di) == i) ) yield (i+di)).foldLeft(0){(a,b) => a+b};


Get in touch with us to learn more about the subject and related solutions

Explore related posts