For the last month or so, I’ve been writing blog posts in conjunction with a few other guys. Each week, we pick an open-ended topic, and try to write something about it. It’s been a lot of fun and has been useful for keeping myself motivated to keep writing and to come up with my own opinions about various programming topics. Anyway, this week, we chose “functional programming” as our topic, and so I thought I’d take the opportunity to talk a little bit about Clojure.

I’ve been messing around with Clojure for several months, and I’ve come to like the language a lot. I first started experimenting with Clojure because I wanted to learn how to use Overtone. Overtone is a music programming library for Clojure and is a ton of fun to play around with. I’ve given a few talks about Overtone too - probably the best of which was my Lightning Talk at Pillarcon.

Writing code in Clojure is a lot different than what I’m used to, but there’s a lot I really like about the language. Firstly, Clojure was my first exposure to the sort of “classic” functional programming operations, like map, reduce, and filter. Just having those three concepts in my head has changed the way I’ve approached a lot of problems, and lots of languages have them built in these days. I also have come to like the consistent syntax of Clojure and Lisp in general - left paren, function name, args, right paren, pretty much 100% of the time. A lot of imperative and object oriented languages mix various syntaxes together, sacrificing consistency for readability. There are pros and cons for each approach, but I think a lot of the time I prefer the Lisp syntax - it makes it easy to parse code as you read it. You can find the innermost set of parens, evaluate it mentally, and then rinse and repeat recursively until you reach the outermost braces (that’s what the computer’s doing). I think it makes it easier to break problems down, and it certainly makes it easier to write tests, especially since functional programming tends to minimize side effects and methods that return differently based on program state. You also end up writing code by composing many small actions together, which I think is easier to reason about. One last benefit of writing code in this style is that it’s easy to extract pieces and run them in the REPL - or, if you’ve got it set up, right in your editor.

I edit Clojure in Vim, and there are a lot of great plugins to make it easier. I’m a huge fan of vim-fireplace which adds REPL functionality to the editor (it’s seriously awesome). There’s also vim-clojure-static which adds general language support features to Vim. Depending on your version, this might be built into Vim. I haven’t found a great solution to dealing with the parens - there’s paredit which seems to be the most popular solution, but it also DEMANDS that you keep your braces matched and I have had trouble doing certain operations because the plugin will prevent you from making them uneven. For what it’s worth, it seems to me like paredit is very powerful - I just haven’t really taken the time to learn it well enough for it to do more good than harm.

As cool as Overtone is, it’s basically all I did with Clojure for the first few months that I used it, and I feel like working in a small subset of Clojure has made my experience less general than I’d have liked. I learned a lot of the basics of the language from the 120 Hour Epic Sax Maration which is definitely a great resource for starting out, but I feel like I still have a TON to learn about the language. I’ve only written really basic code with the language so far, and I still need to wrap my head around smart ways to organize code in a functional way. I’m so used to the typical object-oriented paradigms that this kind of bends my brain a little.

The way you model data for a given problem is one example of where functional programming differs from object-oriented. You can kind of approximate the idea of an object by using maps to store data and even functions, but that analogy only takes you so far- for example, let’s say I’m making a Character “object” for the Evercraft Kata, and I start with something like this:

(def character {:name "Nathan Explosion"})

That’s all great, and you don’t have to write getters or anything because that’s just built into hashmaps. But if I want to validate an argument, there’s no way to do that. Clojure has a few different ways of solving this specific problem, such as records and handling state through atoms; my point isn’t that you can’t do things with the language - it’s just that you have to relearn certain things or learn the right way to do things functionally, and that can be tricky.

I still have a long way to go with Clojure, but I’m having a lot of fun learning. I’ve been going through Living Clojure to shore up my skills even further. This is an awesome book if you’re interested in learning - it goes quickly enough to not be boring, but takes you through all the basics and gets into some of what makes Clojure special. It also includes a “training program” with daily problems to work as you progress your skills. I’m also pretty interested in Clojure Applied and will probably be getting a copy once I finish reading Living Clojure - this book seems to be focused on real world examples of Clojure in action, and has a section on testing, which tends to get neglected in tutorials and documentation.

All in all, I would definitely recommend checking out Clojure if you’re at all interested. There’s a great community and great materials to help you learn, and I’ve noticed myself thinking about certain problems in different ways since I’ve started learning a functional language. There’s a lot of exciting things you can do with Clojure - there’s Overtone, frontend and backend web dev libraries like Ring and Reagent (which actually uses ClojureScript, essentially Clojure that compiles to JavaScript), and even interfaces for React Native that can enable writing mobile apps in Clojure. Clojure’s JVM interoperability is also quite powerful and makes it a practical and widely supported language. Most importantly, Clojure is a lot of fun, which I think is the real reason that I’ve managed to stay interested for so long. Go check it out - you’ll get over the parens. And as always, thanks for reading!