Snow 2.0

It snowed again last night and when I woke up the snow was still fresh. Taking more pictures seemed like the right thing to do:

/blog/entries/2011/01/21/snow_2_0/snow1-beac919b08f7.min.jpg /blog/entries/2011/01/21/snow_2_0/snow2-02b3aef69402.min.jpg /blog/entries/2011/01/21/snow_2_0/snow3-4b8bc602d305.min.jpg /blog/entries/2011/01/21/snow_2_0/snow4-6003e76907ba.min.jpg /blog/entries/2011/01/21/snow_2_0/snow5-6117e3d584ce.min.jpg

Snow in Brooklyn Heights

There was some snow in Brooklyn Heights last week. Here are some pictures from when the snow was still fresh:

/blog/entries/2011/01/20/snow_in_brooklyn_heights/snow1-137b99546b11.min.jpg /blog/entries/2011/01/20/snow_in_brooklyn_heights/snow2-944ae63f540b.min.jpg /blog/entries/2011/01/20/snow_in_brooklyn_heights/snow3-fcb2d7cb4529.min.jpg /blog/entries/2011/01/20/snow_in_brooklyn_heights/snow4-773972feb8a1.min.jpg /blog/entries/2011/01/20/snow_in_brooklyn_heights/snow5-24f6bfd61f87.min.jpg /blog/entries/2011/01/20/snow_in_brooklyn_heights/snow6-d2ffdd152fbf.min.jpg

Brooklyn Heights in Photos

I took my new phone out for a walk and took some pictures of the neighbourhood to see how the camera held up. Here they are:

/blog/entries/2011/01/08/photo_tour_of_brooklyn_heights/christmas-tree-f0b7cfc59903.min.jpg

Brooklyn got a wee bit of snow over the holidays. Here's some snow on a used Christmas tree.

/blog/entries/2011/01/08/photo_tour_of_brooklyn_heights/row-houses-a0d97e202133.min.jpg

1800s brick row houses... yay!

/blog/entries/2011/01/08/photo_tour_of_brooklyn_heights/church-bc343625765d.min.jpg

A church.

/blog/entries/2011/01/08/photo_tour_of_brooklyn_heights/church2-5c200b84b73a.min.jpg

Another church, this one's at Montague and Henry.

/blog/entries/2011/01/08/photo_tour_of_brooklyn_heights/fidi-1d7e11afbe33.min.jpg

Lower Manhattan. You can see it from my 'hood.

Scala Templates

I've recently been messing around with Scala and figured I'd tackle a problem I've already solved in other languages. Here is the template kata implemented using a regex to match the placeholders:

class Template(val tmpl: String) {
    def render(context: Map[String, Object]): String = {
        return """\$(\w+)""".r.replaceAllIn(tmpl, m => {
            context.getOrElse(m.group(1), "").toString()
        })
    }
}

Here's a couple things I like:

  • Scala's Maps have a getOrElse method, yay! It's a little more verbose than Python's get method, but at least I don't have to write it my self.
  • It's actually shorter than my first implementation in JavaScript, including the type annotations.
  • I actually really like the class parameter, implicit field, thing (val tmpl: String). It handles the "The constructor takes and argument and binds it to a field." use-case very nicely.
  • Scala's lambda expressions are very concise, and it's hard not to like that.

Here are the tests written in a BDD style using ScalaTest:

import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers

class TemplateSpec extends FlatSpec with ShouldMatchers {
    val EMPTY = Map[String, Object]()

    private def test(tmpl: String, expected: String, ctx: Map[String, Object] = EMPTY) {
        new Template(tmpl).render(ctx) should equal (expected)
    }

    "A template with no tokens" should "return a blank string if the given string is blank" in {
        test("", "")
    }

    it should "return a non-blank string unchanged" in {
        val identity = "hello, world"
        test(identity, identity)
    }

    "A template with one token" should "replace missing values with the empty string" in {
        test("$name", "")
    }

    it should "replace the token with value from the context" in {
        test("$name", "Aaron", Map("name" -> "Aaron"))
    }

    it should "leave the rest of the string unchanged" in {
        test("Hello $name!", "Hello Aaron!", Map("name" -> "Aaron"))
    }

    "A template with two tokens" should "replace each token its value from the context" in {
        test("Hello $name! It is a $attitude day.", "Hello Aaron! It is a wonderful day.", Map(
            "name" -> "Aaron", "attitude" -> "wonderful"
        ))
    }

    it should "replace each missing value with the empty string" in {
        test("Hello $name! It is a $attitude day.", "Hello Aaron! It is a  day.", Map(
            "name" -> "Aaron"
        ))
    }

    it should "replace adjacent tokens with their respective values" in {
        test("$me$you", "AaronJohn", Map("me" ->"Aaron", "you" -> "John"))
    }

    it should "replace every occurrance of each placeholder" in {
        test("$a-$b-$a-$b", "AAA--AAA-", Map("a" -> "AAA"))
    }
}

They're a little verbose, but as I mentioned in a previous post, you can extract documentation from tests like these:

TemplateSpec:
A template with no tokens
- should return a blank string if the given string is blank
- should return a non-blank string unchanged

A template with one token
- should replace missing values with the empty string
- should replace the token with value from the context
- should leave the rest of the string unchanged

A template with two tokens
- should replace each token its value from the context
- should replace each missing value with the empty string
- should replace adjacent tokens with their respective values
- should replace every occurrance of each placeholder

Stack o' Phones

A stack of phones.

To the right: the phones I have acquired since moving to New York City (I had a phone before I moved to NYC, but I left it in Canada):

  1. The shitty pre-paid Nokia phone I bough while I was apartment hunting.
  2. My venerable MyTouch 3G. I got after I had an apartment, but only just: Verizon hadn't managed to turn on my Internet nor had I yet managed to purchase a bed.
  3. The Windows Phone 7 phone I got at PDC.
  4. The Nexus S that arrived today. I will post more about it later, but the few hours I have spent fiddling with it have been full of joy.

Yes, that's a crazy number of phones, but I've come to terms with the fact that I will own way too many shiny things with screens.