January 16, 2020

Clojure for the Brave and True

A reflection on one of the most recommended introductory books to Clojure
Tags: books clojure

by Daniel Higginbotham (twitter)


Why did I read this?

My interest in Clojure (and Lisp and Functional Programming) started a couple months ago when I first read about it in Uncle Bob's blog post "Why Clojure?".

In my mind, large systems equated to large syntax. Boy, was I wrong.

-Uncle Bob

Clojure is a hosted language. Its original and most prominently host is the JVM.

I dislike Java. Still, I recognize the value of its ecosystem and am very grateful to the people that allowed the JVM to modernize software. In fact, I wrote my master's thesis in Kotlin (another JVM-powered language). It felt as a more forgiving Java spin at times, and an overcomplicated mess when trying to interop with specific libraries and frameworks.

But, what do I know, I didn't spend time trying to learn Kotlin, I just needed to use some libraries written in it and every step away from plain Java was, in my view, one in the right direction. As far as I'm concerned, it was yet another JVM language that tried to replace Java with a slightly less horrible syntax and better guardrails.

Clojure could have been the same story. But as anyone who scratches the surface of Web resources on the matter, I quickly wound up listening to its creator, Rich Hickey, rant about how things are done wrong in Java, and how this drove him to create Clojure.

Java libraries in a language that was concieved in stark opposition of the Java mindset? Right on!



The book is split in two. The first part is a really (sometimes too much so) gentle introduction to the language. Syntax, toolset, constructs, and the ideas behind them. This book was my first contact with Functional Programming, and many of the explanations and examples (which are sometimes delivered in contrast with the Object Oriented model) were very easy to follow and internalize, albeit the sometimes quirky examples and illustrations. I quickly understood Clojure is a language on its own, and the Java interop is not its main focus (as in, Java libraries with a nicer syntax), but just another feature that contributes to the language's "reach". It is totally Ok to instantiate objects when state is needed. This said, I skipped a couple chapters that I considered as filler: I don't need to learn how to use a text editor, nor a command line. I guess this just makes the book a lot more approachable to new programmers. I can see the appeal a couple years ago, but today one can confortably write Clojure (nREPL support and parenthesis handling) in any major editor.

The most valuable things I learned from the first part is that Clojure is at its most efficient when treated like Lego blocks, with a specific philosophy centered around simplicity (non-compoundness). The book helps demonstrate this by introducing very powerful primitives which one (the reader) can organically use to figure out how the more complex ones are built. Second is the language's expressiveness. The separation of reader and evaluator is well explained, and although I'm not writing macros yet i feel I got a pretty good understanding of this and other core concepts in the lisp family of languages.

The second part, "Advanced Topics", introduces some core libraries along with the concepts necessary to understand their purpose. Constructs are discussed such as thread handling, jvm interoperability, and polymorphism.

Although well presented and thoroughly explained, one needs the time to internalize them and probably find a pet project to play with them. But this is a subjective opinion. The presentation is again bottom-up, and full of quirky but relatable " ELI5 " examples before diving into the details and modus operandi. I will admit I have to read this part again, as I didn't do all the exercises (not easy to whip out the laptop in public transport). I'll update the post after I do, as a way to force myself into it.

In summary I could hardly have found a more entertaining way to get introduced to the language. The book re-ignited my interest for learning new concepts, as I have seldom heard advocates in other languages describe simplicity and maintainability to the extent Clojurians have.

Future reading

Since reading Clojure for the Brave and True I have indulged in the following material, which I will hopefully read at some point.