Idiomatic Functional Programming: The Importance of Immersion
Today, I had the privilege of pairing with a colleague who is also focused on learning Clojure. While I come from a background in object-oriented programming, his is in functional programming. Getting a chance to work together and compare how we would each approach different problems was enlightening.
For instance, one problem that we worked on involved trying to design a function that packs a sequence:
I had already developed a working approach to this problem, but I wasn’t too happy with the result:
It’s so ugly that it doesn’t really merit further comment. Suffice it to say I’m basically replicating a for
loop where a set of variables are slowly pieced together to generate the data I’d like to return.
Letting my partner do the driving, we came up with a much nicer solution:
The key to this (much more elegant) solution is it’s effective use of the take
and drop
functions - something we found relevant in approaching numerous other problems:
The lesson I’ve taken away is that learning new things in programming means fully immersing yourself in the idioms of the language/model with which you’re working. With a bit of creativity, it’s usually possible to replicate familiar ways of solving problems - even if they take away your for
loops and mutable data. But replicating old ways of doing things stands in the way of learning other ways that are new and better. In this case, I’m super happy to have had the chance to pair with a colleague able to show me some new and better ways of doing things.