Skip to content

Instantly share code, notes, and snippets.

@matlux
Last active January 2, 2016 12:09
Show Gist options
  • Save matlux/8301616 to your computer and use it in GitHub Desktop.
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.
(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