Week 4 Day 1 – Scala time!

Now i come to another language that i know very little about. Here’s what i do know:

Scala is a sort of bridge between object oriented and functional paradigms. Much like C++ was a bridge between procedural and object oriented. These bridge languages allow two paradigms to coexist, providing a comfortable switch over for the programmer. In this stage of Seven Languages in Seven Weeks, we’re also getting a comfortable shift into the pure functional languages to come later.

Installing Scala

I know that Scala runs on the Java virtual machine, so i was expecting a hard time installing. Not so at all.

$ brew install scala

Homebrew downloaded it, probably added a symlink somewhere and that was it! Good to go!

Scala first impressions

No semicolons! woohoo!

This res1, res2 etc is interesting. i didn’t think on it too much until i got to assigning a variable:

scala> 5 != 2
res16: Boolean = true

scala> val a = 1
a: Int = 1

scala> 1 + 1
res17: Int = 2

scala> res17
res18: Int = 2

I noticed that we got ‘a’ instead of res17. Then i tried something else and there was res17. Then i figured out that res17 is a variable. I guess that’s useful to be able to look back and use a previous result.

There is a nice early hint at the way the object oriented and functional paradigms exist happily side-by-side: var is mutable, and val is immutable. Perfect!

I see that Scala takes a while to get started. I suppose it is compiling as it goes.

The book shows us how to define classes and instantiate objects. After our adventures into Io and Prolog, this feels a bit old-school. I’m looking forward to seeing Scala’s functional side.

My initial reaction is that i like Scala, and i certainly much prefer it to Java.

Exercise: tic-tac-toe

Or as i like to call it, zeros and crosses. What do tictacs and toes have to do with this game?!

Immediately on encountering this, i remember the Prolog sudoku solver and think, “Surely this problem would be better solved with Prolog?!” :)

Okay, so i have to take in a board and check whether somebody has won. I suppose i need an array of nine elements representing the squares on the board. In Scala, an array is called a List. Let’s start with the easiest case: an empty board and nobody has won.

class Tictactoe(val board: List[java.lang.String]) {
  def winner {
    println("Nobody won")
  }
}

val newGame = new Tictactoe(List("", "", "", "", "", "", "", "", ""))
newGame.winner

And to check that it runs:

$ scala tictactoe.scala
Nobody won

Okay, now we need to put a different message if somebody has won. So imagine the board

X | X | X
- - - - -
  | 0 |
- - - - -
  | 0 | 0

I am not known for my ascii art, by the way ;)

So a crude way to solve this is to check the first three elements, if they’re all the same, and they’re not empty, then that person won.

class Tictactoe(val board: List[java.lang.String]) {
  def winner {
    if(List(board(0), board(1), board(2)).toSet.size == 1
        && board(0) != "") {
      println(board(0) + " won!")
    } else {
      println("Nobody won")
    }
  }
}

val newGame = new Tictactoe(List("", "", "", "", "", "", "", "", ""))
newGame.winner

val xShouldWin = new Tictactoe(List("X", "X", "X", "", "0", "", "", "0", "0"))
xShouldWin.winner

I carried on like this for a little bit. I extracted a Triplet class for checking trios of square values within a board, to see if they were all the same.

I was pleased to find that, like Ruby and Io, you can return early from a method.

I’m not very proud of my code, but i’m not going to refactor too heavily tonight as i don’t yet know the Scala idioms. So here it is: week4-scala/day1/tictactoe.scala

Summary

It feels like Java but not. A good Java feel is its strong type checking. At some point i was writing a method that was to return a boolean and i’d done an if clause without an else. It caught that for me at compile time, to make sure i could guarantee a return value. That’s one of the things i like about Java.

But it feels very much easier to write than Java. I felt ready to get started straight away, with very little syntax cruft to hold me back. So i think i’m going to enjoy this Scala week!

Advertisements