Log in

No account? Create an account
Previous Entry Share Next Entry
Programming as art: Scala's new Collections API
Okay, this one's for the hardcore code geeks in the audience; anybody else will find it eye-glazing. But if you really love code, check out the Scala 2.8 Collections API. It's a thing of beauty, and a really good illustration of why I so love Scala -- they systematically deconstructed every major collection type, figured out exactly what they have in common and what makes each one different, and rebuilt the libraries to be about as consistent and sensible as is humanly possible. (They were already way better than average in 2.7, but they nonetheless rewrote it under the hood to make it all *right*.)

The result looks about as close to perfect as you can get: as many common base traits as possible (making everything more learnable and robust); focused on immutable collections (for safety and scalability) while allowing you to work with mutable ones whenever you feel the need; lots of consistent operations, so very-high-level functional programming Just Works; abstractions that let you seamlessly work with infinite collections exactly the same way you do finite ones; thoroughly type-safe from top to bottom, using Scala's type inference to catch programming errors without you needing to state types explicitly. I could go on, but you get the idea.

I sometimes talk about programming as my artform, and I mean that quite seriously: I perceive the aesthetics of code just as clearly as I do for painting or music. The Scala project has won me as an adherent on that basis, perhaps more than any other -- there is a common devotion to crystalline elegance in both the language and its tools. That lets me program *better* on a practical level, but beyond that, it's just plain more *satisfying* a language to work in...

  • 1
Thanks for the link! This carries more weight for me than many of the "hey, shiny feature!" or "look at this clever trick!" articles I've read. (Although I'll admit that the sleight-of-operator-ing which makes s += 4 work similarly for mutable and immutable Sets did make me smile. :)

I've only read up through Sets so far, with a brief jump forward to Views, but look forward to perusing the rest.

(Question: The Sets page mentions the intersection operator as
xs & ys
and the difference operator as
xs &  ys
which appears to me as if it differs only by whitespace. Is there some font/display issue not showing me a ~ or a ! or something?)

(Although I'll admit that the sleight-of-operator-ing which makes s += 4 work similarly for mutable and immutable Sets did make me smile. :)

Huh -- hadn't even noticed that bit. Useful to know that they're treating += in this way, which I hadn't realized. (I'm starting to really internalize the immutable/functional model, especially now that they have immutable collections that are in the right ballpark performance-wise, so I'm paying attention to things I hadn't before.)

One advance warning: there's a bit of Scala syntax that is implicit in a bit of the discussions, which is *very* quirky but a deliberate design decision -- any operator whose name ends with ":" is right-associative. That is, the object that defines the operator is on the right, not the left. This is very typical of Scala: they had the problem of wanting to allow right-associativity, but wanted to make it consistent across the board. So you'll hit that at least with the "z /: it" (foldLeft) operator on iterators; don't recall if it's used much else in the collections.

Is there some font/display issue not showing me a ~ or a ! or something?

Yeah, I'd noticed that. Looking it up in the online Scaladocs (the actual API reference, generated from the code), it looks like there's a tilde that got scrubbed in the HTML document. That makes sense: translating the symbols to English, it comes out as "and not", which is a reasonable definition of difference...

  • 1