Back

The first steps in Scala

Added: January 25, 2009

Tags: book scala

I "forced" my brother to buy Programming in Scala in December and I got a chance to quickly read a few chapters. It is really interesting language, strange syntax is well compensated with strong features it has. So I have decided to buy one too.

I have started to read it from scratch recently, doing examples and experimenting as readers are supposed to. While doing my experiments I needed to use scaladoc documentation. When I have found method I was looking for, it was a bit shock for me - How am I supposed to read that? And I know what 'transform' should do."

Let's see an example from Map:

def transform[C](f : (A, B) => C) : Map[A, C] 
So I did a few experiments until interpreter accepted my input, then a few more and I know how to read it now :-)
    var x = Map(1->"A", 2->"B")

    x.transform((k,v) => v*2)
//->  Map(1 -> AA, 2 -> BB)

    x.transform((k,v) => v.isEmpty)
//->  Map(1 -> false, 2 -> false)
So, now I understand that method transform takes function with parameters A and B (meaning key and value pair) and transforming it to C. Result C is taken and as result of transform new Map A->C is returned. A is not modified, so it is not specified in transform[C] I guess.

I can see there will be a few more surprises along the way, but if solving all of them feels as good as cracking this little problem, then I will have enjoyable experience in learning Scala.

Note: I decided to give Scala a try after my discussions about it with Peter Misak.

Back