Friday, April 22, 2011 10:21 PM | rahel luethy | 2 comment(s)

Scala Gems #3: Named Parameters

Just a quick one this time (it's easter holiday and the weather is simply too beautiful to waste time in front of a computer). Anyway, have a look at this constructor call:

MigrosEasterEgg(GREEN, BLUE)

Unless you're Swiss and just know that I'm referring to the "Extra" variant, wouldn't it be a lot less ambiguous to write

val aMigrosEgg = MigrosEasterEgg(dotColor = GREEN, bgColor = BLUE)

As of Scala 2.8 you can name your arguments. You can also leave out those which have default values, shuffle their order — or you can simply have another one of those little suckers:

val notAMigrosEgg = aMigrosEgg.copy(dotColor = PINK)

Happy easter everyone!

Thursday, April 14, 2011 8:43 PM | rahel luethy | 3 comment(s)

Scala Gems #2: Getting Functional

As promised, here is the second post in my haphazardly thrown together Scala series. Over the past few days I have read some chapters in Odersky's Programming in Scala, and most importantly, I have written my first ~100 lines of code (a basic Minesweeper app with a simple Swing UI). It's been a lot of fun, and I am still very enthusiastic about many new concepts & constructs. When coding, the biggest challenge is to not fall back to imperative Java style, but really use the functional concepts wherever applicable. I am probably still not radical enough, but for the time being, here is my take on revealing non-mined cells:

In summary: 1 usage of => (and even a recursion ;-)).

Friday, April 08, 2011 9:01 AM | rahel luethy | 2 comment(s)

Scala Gems: #1

This article finally did it: "Guardian.co.uk Switching from Java to Scala" — it made me jump head first into the Scala newbie pool. Of course I wanted to learn Scala for quite some time already, but I somehow never managed to get my act together.

Life as a Java programmer had gotten harder over the last few years. Not that I don't enjoy my job anymore, to the contrary, but reading about all these highly dynamic youngsters hacking together their Python/Ruby/Groovy wizardy at the speed of light has made me feel even older than I already am. Old-fashionedness at its finest: I absolutely can't relate to all this enthusiasm for dynamic languages. Ok, I admit that some script-fu will certainly help getting this nasty little problem solved quickly, but how the heck will a huge Rails project be maintainable over 10+ years? Or do we still have to wait longer for time to tell that it will fail? Do these people write tests for each and every thing that otherwise would be checked by a compiler? Isn't this a rather high price for a little duck-typing?

I am obviously being cynical, but luckily I am not the only one in doubt. Some great brains also think that throwing away the type system is not the best compromise to make in order to get rid of Java's (admittedly extensive) boilerplate. In this excellent talk, Bill Venners speaks about his preference for typed languages. He mentions "deterministic refactoring" as the main benefit, which he explains like this:

I don't like types because I think they proof my program correct [...] but because they can proof changes that I make to my programs are correct

Coming back to Scala: It seems to combine the best of all worlds — strictly typed, functional & object-oriented, radically concise, and just close enough to the Java world for old-timers to transition gradually ;-)

To keep my new Scala heart beating, I will start posting weekly Scala gems. Let me start with the example given in the above interview — just to illustrate what "no boilerplate" and "concise" really mean:

Java:

public class HowdyClass {

  private String name;

  public HowdyClass(String name) {
    this.name = name;
  }

  public String sayHowdy() {
    return "Howdy " + name;
  }
}

and in Scala:

class HowdyClass(name: String) {
  def sayHowdy = "Howdy " + name
}

The second example is my personal highlight after roughly two hours of playing around with the interpreter: Initializing a two-dimensional grid with indexed cells:

class Cell(x: Int, y: Int)
Seq.tabulate(3, 9)(new Cell(_, _))

This is actually almost too concise (or at least as a functional noob it took me quite some time to understand it). Here's a verbose equivalent which is a bit more self explanatory:

Seq.tabulate(3, 9)((x: Int, y: Int) => new Cell(x, y))

And now go get it and have a lot of fun!