Continuing My Scala Adventure

Following up on my previous post, I’m beginning to like Scala more and more. This post summarizes some interesting things I discovered while reading the free Open Feedback Publishing System book from O’Reilly, Programming Scala by Dean Wampler and Alex Payne. Kudos to O’Reilly Media Inc. for the OFPS venture, so we can read and evaluate good books for free!

Coming back to Scala, here are some bits and pieces I’ve picked up from the first chapter of the book mentioned above, that I think are very interesting and useful. The book promotes installing the JDK version of Scala from the language’s official Website, which I caved in and did, despite having installed (and been happy with) the Scala IDE and its Worksheets. I created separate directories for working with the IDE and working with the command line. At this point I am really hoping I don’t have redundant Scala stuff installed on my computer. So here goes —

[1] Scala sbt, the tool I mentioned in my previous post, happens to be a build tool just like Ant or Maven for Java

[2] Some of the main reasons why people should pick up Scala are outlined in the first chapter, namely succinctness, static typing (despite the long standing debate about statically typed versus dynamically typed languages, mostly we all know that static languages are easier to optimize for performance because the types are known in advance), running on the JVM (since the JVM continues to enjoy awesome performance optimizations and allows existing Java classes and libraries to be used with new Scala code)

[3] Scala is very flexible to use since it combines object oriented programming with functional programming, so that programmers can use the best of both worlds

[4] Scala, despite being statically typed, feels and reads a lot like succinct dynamic languages such as Python and Ruby, which is definitely another attractive factor

[5] In Scala certain things are really easy, e.g. creating a Singleton is as simple as writing object instead of class, and Scala takes care of the rest and makes sure that there will only ever be a single instance of the class

[6] Concurrency is apparently done very well in Scala. The concurrency model in Java is error prone; but in Scala, just because there are no shared mutable states, there is no need to synchronize any states like that, which yields much cleaner and simpler concurrent code. It is modeled after the Actor concurrency model in Erlang, which translates to a bunch of software entities with no shared state information, communicating by exchanging messages. I really need to wrap my head around this thing.

So, off to learning some concurrency!

Getting Started with Scala

Scala IDE Worksheet

Scala IDE Worksheet

Martin Odersky over at the EPFL in Lausanne, Switzerland, has recently started the first offering of his course Functional Programming Principles in Scala on Coursera. It’s a seven-week long course, with weekly videos and programming assignments. I like it so far, as all the others from Coursera.

I do have some experience with functional programming – Haskell and Clojure to be specific. But I have to say, I like Scala better than both of them. Here’s why.

Scala and Clojure both have the advantage of being closely associated with the JVM; something that Haskell does not enjoy – which makes it less attractive for professional development. Furthermore, between Scala and Clojure, Clojure belongs to the Lisp family, and requires the use of an infix notation and lots of parentheses – while Scala does not.

Scala feels like a semicolon-less Java, and much much less verbose than the notoriously verbose Java. Java functions are directly callable from within Scala code. The Scala IDE, which is a stripped down Eclipse Indigo optimized for Scala, contains a distribution of Scala and works well. It comes with the Scala Worksheets (see attached figure for an example), which are like a REPL but better. You write your expressions, press ‘Save’, and voilà, the expressions are evaluated for you. I mean how neat is that?

With all this praise, there has to be some nitpicking as well, to keep things balanced in the world. I am not too fond of sbt, the Scala Simple Build Tool, that the course requires to be used for submitting the assignments. I am not knowledgeable enough to conjecture whether it is necessary or whether there could have been a simpler way. My problem with sbt is that every time you run it in a directory for the first time, it appears to download the world from the Internet, before it can start. Then, you have to type console in it to start the Scala interpreter. Given that the IDE already comes pre-equipped with a distribution of Scala, and the Worksheets are already a REPL, and there’s another version available from the official Website of the language, sbt just manages to come in and confuse the heck out of me. Maybe as I delve deeper into the language things will change and I will understand better.

As far as the language itself is concerned though, it is pure gold. To add to all the qualities listed above, going parallel with Scala is apparently as simple as adding a .par to any variable or object you create, and it’s easy to use Actors to manage more complicated parallel tasks. I cannot wait to learn more and get my hands dirty. Happy Scala-ing!

PS: Update: I’ve played with Eclipse (the Scala IDE) for a few days now and it has crashed on me several times. According to the gentlefolk over on StackOverflow, it seems to be a universally known problem. I came across a supposed bugfix which I tried, and then there was an update for the IDE as well, but none of that changed much. Further according to StackOverflow and this post here, apparently the way to go is IntelliJ IDEA with the Scala plugin. Whatever may be, for now I am sticking with Sublime Text 2 (since Vim by default does not recognize Scala) and SBT as the build/ test tool. I am also using the Scala version from the official Website for smaller stuff. Nothing is as snappy and awesome as the Scala Worksheets though in the IDE. Hopefully some future update will fix the problem.