Collections, a Gateway Drug

by Matt Cholick

I found myself wondering where Groovy's fold left operation was today and it was a pretty striking moment for me as a developer.

When I was an undergraduate, the functional programming class I took was one of my two toughest courses. It was a really alien way of thinking that I just couldn't wrap my mind around easily. At this point, it's hard to know if it was somehow inherently more difficult or just different, as I'd already been exposed to so much programming in a different paradigm. Regardless of the reasons, it was tough. Even simple algorithms took a lot of thought. A recent contrast I read between functional programming and the more familiar paradigms is that when I'm writing in an object oriented or procedural style, I can start coding and think as I go. Functional programming, on the other hand, requires thinking about a problem thoroughly before even touching the keyboard. This certainly rang true for me at the time.

With all that, it was a little bit of a shock to me that I was really annoyed when the code-popup on a Groovy collection didn't show a fold method (Groovy actually has it, they just called it inject). Some of the functional ideas, once quite strange, are now the immediately obvious solutions to solving particular problems.

It all started with collections. As an undergrad, I recalled wondering at the time why the functional folks were so hung up on lists. Lists didn't seem all that exciting. After several years of development, though, I feel very differently. So much of the code I write is manipulating collections in some way or another and so much of it is done in ways that just feel awkward. The functional ideas have been there, in the back of my head, as much more elegant solutions to what I was doing.

It started with a bit Guava a few years back. Google's library lets me work some of the functional ideas into Java's collections. Filtering or transforming a collection is now possible, but the syntax ends up being ugly. It's enough to know that, after writing something with the Guava libraries, the Java language is constraining me. It's missing something - something important. It's missing something I once found difficult and alien but now see as obvious and elegant.

Scala, I'm finally ready for you now.