The long way through Software Craftsmanship

Destructuring as a refactor in Clojure

Apr 14, 2015 - 1 minute read - Comments - clojurerefactordestructuringparallel-change

Manuel has taught us today about the default value while destructuring:

The following example illustrates the use of an :as directive to bind a local with the entire map.

user=> (def point {:x 5 :y 7})
#'user/point

(let [{:keys [x y] :as the-point} point]
         (println "x:" x "y:" y "point:" the-point))

x: 5 y: 7 point: {:x 5, :y 7}

We’ve now seen the :as directive used for both vectors and maps. In both cases the local is always assigned to the entire expression that is being destructured.

Source

This has been used for a method like this one:

(defn all-access[k]
  (do (:a k))
  (do (:b k)))

this has been refactored to this

(defn all-access[k]
  (let [{:keys [a b] :as k}]
	  (do a)
	  (do (:b k))))

the best thing about this is I can introduce the keys to the array while keeping the original map (k) and, when I’m ready, to change some of the accesses to k to its destructured variables.

Disclaimer about AI/GenAI

As of 2026-05-06, the text in these articles and blog entries has been written without AI/GenAI, except I sometimes use a spellchecker to fix errors. Think Word's spellchecker, not ChatGPT.

Notes, as of today (2026-05-06):

  • No code snippet has been automatically generated, nor vibe-coded, nor generated and reviewed.
  • I don’t have any article with AI contribution.

For future entries:

  • I may have used GenAI for the code in the repo. The code I exemplify/copy in the article will always be reviewed and tested, not vibe-coded. I will specify it in each snippet or at the top/bottom of the article.
  • I normally don't use it for the text contents, although if I have used it for the article text, it would be indicated as such.

Any entry before 2026-05-06 does not contain any AI/GenAI.

For more information, read the AI/GenAI Policy

Redesign as a new TDD phase Inserting clojure code in octopress