Destructuring

Destructuring allows us to conveniently bind elements of collections to symbols

(def s [1 2 3])
 
;with destructuring
(let [[x y] s]
  (do-stuff x y)) ; (do-stuff 1 2)

(def s [1])
 
;with destructuring
(let [[x y z] s]
  (do-stuff x y z)) ; (do-stuff 1 nil nil)

(def m {:y 3 :x 8 :z -5})
 
;with destructuring
(let [{:keys [x z]} m]
  (do-stuff x z)) ; (do-stuff 8 -5)

  • open/destructuring.txt
  • Last modified: 2024/10/05 06:15
  • by 127.0.0.1