Destructuring Examples
Subscribe to a paid plan to get instant access to this course.
 Video time: 11m10s

Want this course?

Destructuring Examples

We go over some cases where destructuring is very useful. We also look at why we shouldn’t use it all the time.


(for [[k v] (seq {:a 1 :b 2})]
  (str k v))

(let [[_ first-name last-name] (re-find #"(w*) (w*)" "Eric Normand")]

(defn part [p? ls]
  [(filter p? ls)
   (remove p? ls)])

(let [res (part even? (range 10))
      evens (first res)
      odds (second res)])

(loop [ls [nil false 1 2 3 4]]
  (when-let [[f & rst] (seq ls)]
    (println f)
    (recur rst)))

(def circle {:center [0 0]
             :radius 5})

(defn area [radius]
  (* 3.14 radius radius))

(defn circle-area [{:keys [radius]}]
  (area radius))

;; some other code

(area (:radius circle))