Thursday, September 27, 2012

Sometime back I registered for the Functional Programming Principles in Scala, on Coursera. I have been meaning to learn Scala from a while, but have been putting it on the back burner because of other commitments. But when I saw this course being offered by Martin Odersky, on Coursera, I just had to enroll in it.

This course is a 7 week course. I will blog my learning experience and notes here for the next seven weeks (well actually six, since the course started on Sept 18th).

The first step was to install the required tools:

JDK - Since this is my work machine, I already have a couple of JDK's installed

SBT - SBT is the Scala Build Tool. Even though I have not looked into it in detail, it seems like a replacement for Maven. I am sure we will use it for several things, however upto now I only know about two uses for it - to submit assignments (which must be a feature added by the course team), and to start the Scala console. Installed sbt from here, and added the path to it's bin directory in my .bashrc file. When I first started sbt, I did not realize that sbt would need to connect to the Internet to download it's dependencies. My computer was disconnected from the Internet, which caused sbt to give up. This is the first time I realized that sbt seems to be doing something that I would expect Maven to do.

Next, I installed the Scala IDE, which seems to be Eclipse with a Scala plugin. Since I already have Eclipse, I wonder if I cold have simply installed a plugin.

Once I had the tools setup ready, I created a workspace for all my DIYLearning courses (since I plan to always be enrolled on at least one course, from Coursera, Udacity, edX, or any other open courseware offering that is good).

Then I downloaded the example project, made a few modifications to it, ran unit tests and submitted it. I learned quite a few things by wandering around this example project.

Directory structure is similar to Maven:
The directory structure of the projects is similar to what it might have been had I used Maven. The sources were in 'src/main/scala', and tests in 'src/test/scala'. sbt seems to have a directory structure similar to Maven. I like that. It does not make sense to relearn everything from scratch for every single tool.

Scala imports are slightly different
This is how Scala imports everything from a namespace. Notice how they have deviated from Java imports by using the '_' character for the wildcard. I asked a question on the forum, and was told that functional programming languages by convention use the '_' character for wildcards. So ok, that too makes sense.

import Lists._

Unit tests in Scala
Unit tests in Scala look similar to unit tests in Java, but there are a few things which stand out as different. Scala uses ScalaTest, as the unit testing tool in Scala. To create a ScalaTest test case, we have to extend FunSuite. Their website also mentions something about traits. I have heard this term before, but do not really know what it is. Need to look into what it really means. I also noticed that ScalaTest's have only one type of assert, which is equivalent to assertTrue. This also makes sense to me, because everything can be reduced to an assertTrue assertion.

ScalaTest introduces an operator called 'test'. This operator takes two arguments, the first one being the name of the test, and the second one being the body of the test. The body usually consists on an assert. Here is how a test operator looks

test("testing equality")(assert(1==1))

The body of the test can also be given as a block, which makes it more readable.