philipbohun.com

Glojure by Example: Destructuring

Binding forms can pull a collection apart. In a vector binding, & gathers whatever is left.

(let [[a b & more] [1 2 3 4]]
  (println a b more))

Map binding with :keys binds locals from keyword entries. :or supplies defaults. A function parameter can destructure the same way.

(let [{:keys [name city]
       :or {city "unknown"}} {:name "Ada"}]
  (println name city))

(defn area [{:keys [w h]}]
  (* w h))

(println (area {:w 3 :h 4}))

The same forms work in let and in parameter lists.

$ glj destructuring.glj
1 2 (3 4)
Ada unknown
12

Next example: Variadic Functions.