Alexander Oloo

Destructuring keys in clojure

Pulling keys out of a map, over and over — and the one bit of syntax that ends it.

Oh, you think JSON is your ally? I do a lot work with JSON. So much so that my favourite library is a JSON parsing library (cheshire). The most tedious thing about working wiht JSON is pulling keys out of JSON objects over and over again.

An animated GIF stood here →

Associative destructuring is the hero we need but don't deserve. It's syntactic sugar for extracting keys from an object in a way that makes code more readable. More readable code is more maintainable code. It's made my life much easier.

Given the following map, that was created by parsing a JSON object using cheshire:

(def json-object {:fname "Alexander" :lname "Oloo" :handle "alekcz" :location "ZA"})

The standard approach to using data from within the map is as follows:

(defn twitter-link [user]
    (str "<a href='https://twitter.com/" (:handle user) "'>" (:fname user)  " (@" (:handle user) ")</a>")))
(println (twitter-link json-object))
; => <a href='https://twitter.com/'>Alexander (@alekcz)</a>

But when we use associative destructuring we get:

(defn twitter-link [user]
    (let [{:keys [fname handle]} user]
        (str "<a href='https://twitter.com/" handle "'>" fname " (@" handle ")</a>")))
(println (twitter-link json-object))
; => <a href='https://twitter.com/'>Alexander (@alekcz)</a>

Isn't that so much nicer? I think it is. It's a pity I didn't see transit until I was already a man.

May your build always pass.

Alex

This post is part of the "Advent of Parens".


← Back to all writing