Created
July 8, 2015 09:27
-
-
Save msgodf/b108c0c79f5245fa3f30 to your computer and use it in GitHub Desktop.
Understanding how lists get destructured as maps in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; @andrew_jones mentioned this from Miller and Vandgrift's Applied Clojure book | |
;; unusual how the varargs were being destructured as if they were a map | |
((fn [& opts] (let [{:keys [a b c]} opts] (str a b c))) :a 1 :b 2 :c 3);;=>"123" | |
;; opts is a list, not a vector | |
(let [{:keys [a b c]} '(:a 1 :b 2 :c 3)] (str a b c));;=>"123" | |
(let [{:keys [a b c]} [:a 1 :b 2 :c 3]] (str a b c));;"" | |
(let [{:keys [a b c]} [[:a 1] [:b 2] [:c 3]]] (str a b c));"" | |
;; how does this occur? does list implement something key-value like? | |
(keys '(:a 1 :b 2 :c 3));; | |
;; no - | |
(destructure [{:keys ['a 'b 'c]} '(:a 1 :b 2 :c 3)]);;=> | |
;; gives | |
#_[map__21735377 '(:a 1 :b 2 :c 3) | |
map__21735377 (if (clojure.core/seq? map__21735377) | |
(clojure.lang.PersistentHashMap/create (clojure.core/seq map__21735377)) | |
map__21735377) | |
c (clojure.core/get map__21735377 :c) | |
b (clojure.core/get map__21735377 :b) | |
a (clojure.core/get map__21735377 :a)] | |
;; clojure.core/destructure converts the list to a map, but not the vector |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment