June 1, 2009 at 9:50 pm (Scala, Week 11) (, , , )

After Caleb and I finished setting up a Slicehost account for a site we’d worked on, Micah met with me to talk about my apprenticeship challenges. I have three major projects to complete by the end of next week: a presentation at this coming Friday’s Lunch & Learn, an article on something I’d learned recently as part of my apprenticeship (more formal and expert-level than posts on this blog), and another version of Tic-Tac-Toe, this time in a language I’d never used before. After a bit of waffling on my part among Clojure, C++, Objective-C, and Scala, I finally settled on Scala.

I’d attended Dean Wampler’s excellent talk at this past weekend’s Chicago Code Camp, so I had some basic syntax knowledge coming in, and there are a lot of similarities to Java. But I thought I should spend some time getting a decent handle on the basics of the language before diving into Tic-Tac-Toe. I discovered what looks like the best test framework for Scala: ScalaTest. It’s pretty nifty, allowing several styles of testing, including one that looks suspiciously like RSpec. There’s also an IntelliJ plugin for Scala, which works well. Since I’m still getting the hang of all of IntelliJ’s features and options, it did take me awhile to get the IDE set up to run tests and write code, but by the end of the afternoon, I had my first failing test.

Things were pretty slow going since I was having to look up basic syntax at first, but I’m already seeing some really cool things in Scala. For one thing, I can mix functional and object-oriented ideas in the same program. It’s hard for me to think about things like a Board outside of an object-oriented space, which makes Scheme difficult for me (so far), but Scala deals with classes and objects without problems. My first implementation code (on the board class) looked basically like Java with a weird syntax and a fancy & functional map method:

def full: Boolean = { =>
     if(position == null)
       return false
   return true

And I thought this was pretty concise, so I was fairly proud. But I did some reading later this evening, and now I’m really psyched about the refactoring Scala allowed me to do:

def full: Boolean = {
  !positions.exists(_ == null)

This seems more like Ruby, but with a really beefed-up version of the Symbol#to_proc syntax that we get with Rails (1.1+) or Ruby 1.9. Nice, right?



  1. Eric said,

    So is _ == null a pattern match where _ is anything?

  2. trptcolin said,

    OK, I should’ve explained that better:
    !positions.exists(_ == null)
    The expression in parentheses is a closure that is passed to #exists as a parameter, and that closure takes one argument. _ stands for the one argument. Apparently if there are multiple arguments to the closure you can use _ multiple times to stand for each of them in turn.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: