Last active
January 2, 2016 12:09
-
-
Save matlux/8301616 to your computer and use it in GitHub Desktop.
Chain map keys macro. This enables you to navigate hierarchically inside the a map of maps (imbricated maps) using string keys as if you'd used keywords.
This file contains hidden or 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
(ns chain-map-keys.core) | |
(def mymap {"key1" {"key2" {"key3" "value3"}}}) | |
;(--> mymap "key1" "key2" "key3") | |
;; => | |
;(-> (mymap "javahome") (get "key2") (get "key3")) | |
(defmacro --> [m firstkey & keys] | |
(let [a (map #(list 'get %) keys)] | |
`(-> (~m ~firstkey) ~@a))) | |
;; test expansion | |
(macroexpand-1 '(--> mymap "key1" "key2" "key3")) | |
(--> mymap "key1" "key2" "key3") | |
;; => "value3" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment