4Clojure with Me
I’m currently in the process of a renewed effort to become proficient with Clojure. We’ve been using it at World Singles in production for a couple of years now, but there’s been enough CFML that I’ve not really needed to do much in the Clojure beyond very superficial updates. But the CFML is shrinking, the Clojure is growing (well, not really… the Clojure code is much more concise than the CFML code, but you know what I mean), and it’s time for me to fully embrace Clojure.
It’s not that I haven’t tried over the past couple of years. But making the leap from self-taught ColdFusion developer to Clojure and functional programming has been no small task. Functional programming is such a different paradigm that I’ve stumbled over many of the basic concepts. Recently, however, I took a Coursera course on Programming Languages. That’s when the light bulbs started coming on. I had read Clojure books. I had gone to conferences. But sitting down and actually writing out code for the assignments, that’s when it started to click. To be clear, the Coursera course wasn’t Clojure-based. It wasn’t meant to teach any particular language. Rather, it was meant to teach programming concepts. And specifically, functional programming concepts. I started to understand the difference between recursion and iteration. The notion of variable assignments, and in particular, re-assigning a value to a variable, started to make me uneasy. Conceptually, the pieces were falling into place.
Now I’m tasked with fleshing out some code at work that relies on Clojure. Some of it is still CFML written in FW/1, but the meat of it is using the Clojure-based Expectations testing framework. It’s been a challenge, but as with the Coursera course, jumping into the code has proved to be very beneficial. But I still need to hone the Clojure skills in order to work efficiently. Enter 4Clojure.
4Clojure, for those who may not know, is an amazing resource for learning Clojure. You’re tasked with writing functions that force you to scour the docs and to understand core functions. Functions that, for example, reverse a collection. So
[:a :b :c] becomes
[:c :b :a]. Sounds easy, right? Clojure has a reverse function, after all. Ah, not so fast. There’s a restriction for this particular problem that you’re not allowed to use reverse. Likewise, there’s an “Nth Element” problem. Given
(= (__ '(4 5 6 7) 2) 6), write the function that fills in the blank. In this case, the function would take a collection (the list ‘(4 5 6 7)) and an integer (2), and return 6. And of course, you’re not allowed to use Clojure’s nth function. It’s often frustrating, sometimes fun, but always educational.
Before starting this time (I had previously started and abandoned the effort), I set up a Github repo for my solutions. There is no shortage of repos with solutions to 4Clojure problems, but I think mine is a bit different. Rather than just post the answers, I’ve also made efforts to thoroughly explain why I went with that answer. Or in some cases, what other answers I had considered first and why I didn’t choose them. I’m hoping that adds some value to the files, and that others can look and learn from them. I also wouldn’t mind terribly if people forked the repo, added their own answers/explanations, and sent me pull requests. There’s no reason that this can’t be more of a collaborative effort.
Whether you fork or not, feel free to peruse my solutions. I’m currently up to problem 25. Some of the answers I’m proud of. Others not so much, but they work. Feel free to post comments here if you have other solutions that worked for you, or questions about any of my solutions.8 comments | 3,750 views