clojure Clojure destructuring Destructuring a map with default values


Sometimes you want to destructure key under a map which might not be present in the map, but you want a default value for the destructured value. You can do that this way:

(def my-map {:a 3 :b 4})
(let [{a :a
       b :b
       :keys [c d]
       :or {a 1
            c 2}} my-map]
  (println a b c d))
  ;= 3 4 2 nil