Last active
October 25, 2024 16:52
-
-
Save burbma/6d1cf376784c381765c3b6b863406773 to your computer and use it in GitHub Desktop.
Clojure sym-map variants
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
;; Credit (Chad Harrington)[https://github.com/chadharrington] from whom I copied | |
;; `sym-map` and who thus inspired the others. | |
(defmacro sym-map | |
"Builds a map from symbols. Symbol names are turned into keywords and become the | |
map's keys. Symbol values become the map's values. | |
(let [a 1 b 2] | |
(sym-map a b)) ;; => {:a 1 :b 2} | |
" | |
[& syms] | |
(zipmap (map keyword syms) syms)) | |
(defmacro sym-nsmap | |
"Builds a map from symbols. Symbol names are turned into keywords, using `n` as the | |
namespace, and become the map's keys. Symbol values become the map's values. | |
(let [a 1 b 2] | |
(sym-nsmap :foo a b) ;; => {:foo/a 1 :foo/b 2} | |
(sym-nsmap \"bar\" a b)) ;; => {:bar/a 1 :bar/b 2} | |
" | |
[n & syms] | |
(zipmap (map #(keyword (name n) (name %)) syms) syms)) | |
(defmacro nssym-map | |
"Builds a map from symbols. Symbol names are turned into keywords with the first | |
hyphen delimited part becoming the ns of the keyword that becomes the map's keys. | |
Symbol values become the map's values. | |
(let [my-a 1 your-a 2] | |
(nssym-map my-a your-a)) ;; => {:my/a 1 :your/a 2}" | |
[& nssyms] | |
(zipmap (map #(apply keyword (str/split (name %) #"-" 2)) nssyms) nssyms)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment