What I Learned Today

I read most of the new JSMag today. The whole thing is good, but I especially liked Nick Carter's article on JazzRecord and Rebecca Murphey's article on using object literal syntax to organize your code. I learned about PhoneGap from it, which is stupid awesome.

zeaLOG is a fun site I found for keeping track of personal metrics. I'm thinking about using this for motivation.

I watched Bill Venners' talk, "The Feel of Scala" this afternoon, and found it pretty enjoyable. The one major take-away was this gem:

Ok, so arguments to a method in Scala can be specified via curly braces instead of parentheses if you want. I did not know that. Also, methods can be defined in such a way that they are automatically curried, which you can see in the example below. (Note: all examples copied from/inspired by aforementioned slides.)

// Not curried
// Example: expect(1, 2 - 1) =>
//          expect(1, 2 + 1) => AssertionError thrown
def expect(expected: Any, actual: Any) {
  if (expected != actual)
    throw new AssertionError("Expected " + expected + 
      ", but got " + actual)
}

// Curried
// Example: expect(1)(2 - 1) =>
//          expect(1)(2 + 1) => AssertionError thrown
def expect(expected: Any)(actual: Any) {
  if (expected != actual)
    throw new AssertionError("Expected " + expected + 
      ", but got " + actual)
}

Not that exciting, right? Watch this:

expect(1) {
  2 - 1
}

Since expect is curried, it can be used like a whole new control construct. If you're wondering if you can put multiple lines inside the brackets, you can, meaning you can write tests much like Jay Fields' expections gem for Ruby.

Published: 03 Apr 2009