Skip to main content

My re-attempt at learning clojure

· 3 min read
Brad Taniguchi
Brad Taniguchi

(learn "clojure")

I tried to learn clojure a few years ago after reading this article from circleci. I was impressed with the language, and finally found a reason to really dive into functional programming, because it seemed like something that could be useful for high performance and highly scalable code.

I bought the popular book "Clojure For the Brave and True" by Daniel Higginbotham. (who amazingly provides the book to read for free online, but recommends you pay) and just couldn't wait to get started.

However after a few days of struggling, I started to "lose sight" of whatever perceived advantages I thought Clojure could offer. Reality started to set in once I ran into the following code snippet in chapter 3 of the book, which was where one of the first real examples is given.

(def asym-hobbit-body-parts [{:name "head" :size 3}
{:name "left-eye" :size 1}
{:name "left-ear" :size 1}
{:name "mouth" :size 1}
{:name "nose" :size 1}
{:name "neck" :size 2}
{:name "left-shoulder" :size 3}
{:name "left-upper-arm" :size 3}
{:name "chest" :size 10}
{:name "back" :size 10}
{:name "left-forearm" :size 3}
{:name "abdomen" :size 6}
{:name "left-kidney" :size 1}
{:name "left-hand" :size 2}
{:name "left-knee" :size 2}
{:name "left-thigh" :size 4}
{:name "left-lower-leg" :size 3}
{:name "left-achilles" :size 1}
{:name "left-foot" :size 2}])
(defn matching-part
[part]
{:name (clojure.string/replace (:name part) #"^left-" "right-")
:size (:size part)})

(defn symmetrize-body-parts
"Expects a seq of maps that have a :name and :size"
[asym-body-parts]
(loop [remaining-asym-parts asym-body-parts
final-body-parts []]
(if (empty? remaining-asym-parts)
final-body-parts
(let [[part & remaining] remaining-asym-parts]
(recur remaining
(into final-body-parts
(set [part (matching-part part)])))))))

example call and output

(symmetrize-body-parts asym-hobbit-body-parts)
; => [{:name "head", :size 3}
{:name "left-eye", :size 1}
{:name "right-eye", :size 1}
{:name "left-ear", :size 1}
{:name "right-ear", :size 1}
{:name "mouth", :size 1}
{:name "nose", :size 1}
{:name "neck", :size 2}
{:name "left-shoulder", :size 3}
{:name "right-shoulder", :size 3}
{:name "left-upper-arm", :size 3}
{:name "right-upper-arm", :size 3}
{:name "chest", :size 10}
{:name "back", :size 10}
{:name "left-forearm", :size 3}
{:name "right-forearm", :size 3}
{:name "abdomen", :size 6}
{:name "left-kidney", :size 1}
{:name "right-kidney", :size 1}
{:name "left-hand", :size 2}
{:name "right-hand", :size 2}
{:name "left-knee", :size 2}
{:name "right-knee", :size 2}
{:name "left-thigh", :size 4}
{:name "right-thigh", :size 4}
{:name "left-lower-leg", :size 3}
{:name "right-lower-leg", :size 3}
{:name "left-achilles", :size 1}
{:name "right-achilles", :size 1}
{:name "left-foot", :size 2}
{:name "right-foot", :size 2}]

ref

I'm not sure what originally tripped me up from this code. Maybe it was me going to fast, or getting overwhelmed by all the variables. I spent a few hour or so trying to digest the code, understand it, and ultimatly gave up on learning the language shortly after.