Wednesday, April 3, 2013

7 languages in 7 weeks: Scala day 1

I went to a great talk by Sandro Mancuso at Devoxx UK last week. He mentioned the project he is currently working on being written in Java for the main business flow and Scala for the data manipulation (sorry if I have remembered that incorrectly). This got me thinking it was about time I learnt some Scala. I've dipped in and out of the 7 languages in 7 weeks book; fortunately it has a chapter on Scala. So here I go...

 Googling around and reading the first day the following stands out:

  • Type inference with the safety of static typing (we're all sick of typing types aren't we??)
  • Companion objects instead of static methods
  • Higher order functions (why do we need lambas? Can't we just switch to Scala?)
  • Interfaces, no I mean mixins, wait traits: interfaces with implementation? I think these are great when used well.
  • Tuples and ranges.
So on to my home work!

The exercise for day 1 is to write noughts and crosses. I decided to concentrate on setting up a nice environment for writing scala rather than great code. I started with vim, then sublime but decided I fancied using an IDE. I started with Eclipse but moved to IntelliJ as it looks nicer on a retina screen :) In  addition to setting up an IDE I wanted to do TDD from the start, so I got to grips with ScalaTest.

Here are a couple of example tests:

Here is the main Board object. I could pull out the logic to determine a winner into a class like Game but as this was my first Scala code I decided to keep it simple in a single file.

I've omitted the code that calculates the different types thought it's all available on github. I looked at using a case class rather than a class that extends Enumeration but I haven't been taught case classes yet  so I'll leave that until later.

That's it for day one!

No comments: