Skip to content

Instantly share code, notes, and snippets.

@borkdude
Created March 27, 2017 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkdude/303553dd2b50910fabed1eecb2c47402 to your computer and use it in GitHub Desktop.
Save borkdude/303553dd2b50910fabed1eecb2c47402 to your computer and use it in GitHub Desktop.
Lightweight GraphQL-like map querying
(defn filter-map [m spec]
(reduce-kv
(fn [m* k v]
(if (some? v)
(if-let [m-val (get m k)]
(let [res
(if (map? v)
(if (sequential? m-val)
(mapv #(filter-map % v) m-val)
(filter-map m-val v))
m-val)]
(assoc m* k res))
m*)
m*))
{} spec))
(comment
(filter-map {:a 1
:d 2
:b {:c 3 :e 5}
:f [{:foo 1 :bar 2} {:foo 3 :bar 4}]}
{:a true
:b {:c true}
:f {:foo true}
:foo {:bar true}
:bar false
:baz nil})
;;=> {:a 1, :b {:c 3}, :f [{:foo 1} {:foo 3}]}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment