Skip to content

Instantly share code, notes, and snippets.

@Raynes
Forked from devn/Destructuring Examples.clj
Created June 15, 2010 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynes/439139 to your computer and use it in GitHub Desktop.
Save Raynes/439139 to your computer and use it in GitHub Desktop.
;;; 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 [{:keys [foo bar]} {:a 1, :b 2,}] [foo bar])
; => [1 2]
(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]
;; thanks, chouser.
(let [[a :as [b :as [c & [d]]]] [1 2]] [a b c d])
; => [1 1 1 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment