Skip to content

Instantly share code, notes, and snippets.

@vsnguyen
Last active August 29, 2015 14:21
Show Gist options
  • Save vsnguyen/05b214998ce41103ac0e to your computer and use it in GitHub Desktop.
Save vsnguyen/05b214998ce41103ac0e to your computer and use it in GitHub Desktop.
Clojure: Destructuring
;; Vector
(def my-vector [1 2 3])
(let [[x y z] my-vector]
(+ x y z))
;; 6
;; String
(def my-string "hola")
(let [[char-1 char-2 char-3 char-4] my-string]
(str char-4 " " char-3 " " char-2 " " char-1))
;; "aloh"
;; Map
(def my-map {:x 1 :y 2 :z 3})
(let [[:x x :y y :z z] my-map]
(+ x y z))
;; 6
(let [{:keys x y z} my-map]
(+ x y z))
;; 6
;; List
(def my-list '("X" "Y" "X"))
(let [[list-1 list-2 list-3] my-list]
(str list-1 " " list-2 " " list-3))
;; "XYZ"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment