Created
March 27, 2017 21:00
-
-
Save borkdude/303553dd2b50910fabed1eecb2c47402 to your computer and use it in GitHub Desktop.
Lightweight GraphQL-like map querying
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
(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