Starting out with Scala

Arjan Blokzijl

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) available here.

Read a book
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};
  }
}

Enjoy.

Comments (9)

  1. Boudewijn van Klingeren - Reply

    July 3, 2009 at 2:17 pm

    Hi Arjan,

    I would like to notice that IntelliJ IDEA also has a very good plugin for Scala development. I'm not sure how stable it is, because I haven't played with it that much yet, but it defenitely is worth mentioning.

    Cheers,

    Boudewijn

  2. Arjan Blokzijl - Reply

    July 3, 2009 at 2:43 pm

    Apparently, the stability still leaves something to be desired: http://twitter.com/jboner/status/2402541265 :-).
    I've used it very briefly at the end of last year, and I must say that it had the most impressive Scala plugin of all three (Netbeans, Eclipse, Intellij) at that time in my opinion.

  3. Twitted by bubbl_scala - Reply

    July 6, 2009 at 1:56 am

    [...] This post was Twitted by bubbl_scala [...]

  4. Sean Bowman - Reply

    July 6, 2009 at 5:14 am

    I liked the idea of Scala, but when it came right down to it, the execution just left a bad taste in my mouth. I came to it from Ruby (though I had years and years of Java before Ruby). Definitely much better than Java, but I kept trying to chain collection-related commands together, and they kept spitting back different, incompatible things. Then I spent all my time trying to figure out how to convert between the various collection return types into something useful.

    I know I'm not explaining the problem very well, and maybe it's no longer a problem. I left Scala behind after writing a few things in it about a year or so ago. Maybe if they've figured out the collections, I should give it another shot...suggestions on where to look for help with those collections commands would be appreciated. Maybe I just didn't "get it" my first go-around.

  5. Kieron Wilkinson - Reply

    July 6, 2009 at 1:55 pm

    Sean: That exact collection problem has been is fixed in version 2.8, which should be going final in a few months time. Even though it is not yet final, it is easily stable enough to play around with.

  6. Scala Lifting Off ? - Technology - Reply

    July 10, 2009 at 8:01 pm

    [...] NetBeans seems to be ahead of the curve in its support for [...]

  7. Paul Phillips - Reply

    July 23, 2009 at 5:30 pm

    In trunk I can express problem 48 like so:

    def bd(x: Int) = BigDecimal(x) pow x ; (1 to 1000 map bd sum) remainder bd(10)

    res0: BigDecimal = 9110846700

    (Hoping that's correct, have no idea...)

    • Arjan Blokzijl - Reply

      July 26, 2009 at 6:20 am

      Correct or not, it's certainly neat and concise, much better than my version, thanks!

  8. [...] NetBeans похоже не собирается отставать от прогрессивного мира, и тоже добавляет поддержку [...]

Add a Comment