My first few days of exploring Clojure
In between many other delightful pursuits, I managed to allocate a few hours each day of my Recurse mini-batch to learning or implementing things in Clojure.
This particular choice of language was semi-arbitrary; my goal was to start learning a Lisp-like functional language, and one of my secondary concrete goals involved visualization as a side effect, so I went with whatever seemed to have the most convenient libraries, which was Clojure – reputedly near-interchangeable syntactically with ClojureScript?
Here are my collated notes from that exploration, which in no way reflect my recommendations for how a beginner ought to approach this subject, as I am still very much one.
Dev setup
Broken out into a separate post.
Online exploration
On Day 1, I wasn’t able to set up the Clojure CLI for mysterious reasons which would become tragically apparent about 24-30 hours in the future (when I nuked my package manager), so I did a few hours of initial learning in online environments.
For a light introduction to the syntax and concepts, I went through several units of ClojureScript Koans in concert with the documentation. This was a nice introduction to expressing things like inline higher-order functions in Clojure.
I also fiddled a little with online REPLs that provided pre-made examples, such as Quil, a library for animated graphics and visualizations, analogous to Processing.js.
Thinking in Clojure
My brain has been shaped by the C-like languages and scripting languages I’ve dealt with most of my life. Although I have aggressively functional-lang preferences when using scripting languages, they are multi-paradigm enough that one can easily fall back to the imperative/OOP style.
Clojure is my first Lisp-like language, so I wanted some philosophy and examples on how to think in this paradigm before stumbling around blindly. Here’s what I gleaned from the first chapter of The Joy of Clojure (2nd ed.), which I dug up in the RC library.
Infix order of operations
The Joy of Clojure blew my mind as early as page 12 with an example of how the order of operations can be elegantly implemented in a functional lang by assigning operation weights and shifting the evaluation order as needed.
Let’s say we want to implement the order of operations for infix operators. Since Clojure uses prefix operators, we first to be able to handle infix expressions in both directions. The following functions simply translate infix expressions into prefix expressions which can then be evaluated in Clojure.
Once we have those, we can quickly implement the order of operations.
We only have addition and multiplication in this example, but we can easily add more operators as needed by extending the map.
This feels really elegant compared to implementing an order-of-operations evaluator in a verbose imperative language like Java, where you need to imperatively reconfigure your infix notation expression substring-by-substring into a nested stack of expressions anyways – with an end result reminiscent of a Clojure-like explicitly nested arithmetic expression!
Clojure philosophy
Paraphrased, a bit of Chapter 1 philosophy behind the language design.
There are many different styles of functional programming. Clojure was specifically created to address frustrations with the weaknesses of object-oriented programming (OOP) in facilitating concurrent programming. One language characteristic which addresses this is strict immutability, allowing objects to be shared without fear of concurrent state modification. Unlike OOP, where state and identity are conflated into mutable state:
Clojure’s implementation attempts to draw a clear separation between an object’s state and identity as they relate to time.
A nice grammatical analogy: an OOP mindset encourages you to define an application domain in terms of nouns (classes), while the functional mindset encourages the composition of verbs (functions). Many of what we perceive to be classes may be expressed as data tables in Clojure (p.24).
Local encapsulation example
Here’s another neat code example from Chapter 1, where we look up a chessboard square by rank (rows, labelled 1-8) and file (columns, labelled a-h).
Note how information that would ordinarily be composed into temp variables and for loops in in a C-like language are instead heavily encapsulated into inline functions.
Gripe: I haven’t yet gotten the knack of reading Clojure quickly. If functional langs are a purer translation of human-to-machine thought than imperative langs, then I dearly wish code examples came better commented than a lot of the relatively uncommented open source I’ve been looking at, because brains structure things in wildly different ways.
Distractions
Overtone: Oh no, someone wrote an open source synthesizer toolkit in Clojure! Bookmarking this for later.