Skip to content

Instantly share code, notes, and snippets.

@devn
Created June 15, 2010 09:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save devn/438897 to your computer and use it in GitHub Desktop.
Save devn/438897 to your computer and use it in GitHub Desktop.
Destructuring Examples
;;; All Kinds of Destructuring ;;;
(let [foo 1] foo)
; => 1
(let [[foo bar] [1 2 3]] [foo bar])
; => [1 2]
(let [[foo bar & baz] [1 2 3]] [foo bar baz])
; => [1 2 (3)]
(let [h {:a 1, :b 2, :c 3, :d 4}] [(keys h) (vals h)])
; => [(:d :c :b :a) (4 3 2 1)]
;; d is the whole input collection [1 2 3 4]
(let [[a b c :as d] [1 2 3 4]] d)
; => [1 2 3 4]
(let [[a b c :as d] [1 2 3 4]] [a b c])
; => [1 2 3]
;; via chouser.
(let [[a :as [b :as [c & [d]]]] [1 2]] [a b c d])
; => [1 1 1 2]
(let [{:keys [foo bar] :or {foo 5}} {:bar 2}] [foo bar])
; => [5 2]
(let [{:keys [foo bar] :or {foo 5}} (list :bar 2)] [foo bar])
; => [5 2]
(let [[& xs] [1 2 3]] xs)
; => (1 2 3)
(let [[x & xs] [1 2 3 4 5]] xs)
; => (2 3 4 5)
(let [{a :a, b :b} {:a 1, :b 2}
[x y] [3 4]]
[a b x y])
; => [1 2 3 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment