aimee's personal journey

Main menu

Post navigation

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:

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.

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!

Post navigation

2 comments on “Week 4 Day 1 – Scala time!”

It seems that scala first day shows us how to use scala as a “better” java (less ; and brackets and voids), but it still has a lot of unnecessary words (winning(board: List[java.lang.String]):java.lang.String reminds me of my java days), even the file extension is too long! :P