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.

Redesign as a new TDD phase Inserting clojure code in octopress