Created
April 5, 2017 18:26
-
-
Save rauhs/d0be89013b7a1b7abe5c6df4c3ed254e to your computer and use it in GitHub Desktop.
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
| (declare pull) | |
| (defn- pull-map | |
| [patterns from] | |
| (reduce-kv | |
| (fn [out key sub-pat] | |
| (assert (vector? sub-pat) "map value must be vector") | |
| (let [sub (get from key)] | |
| ;; sub is now either a set or another entity | |
| (some->> | |
| (not-empty | |
| (if ((some-fn set? vector? seq?) sub) | |
| (mapv #(pull sub-pat %) sub) | |
| (pull sub-pat sub))) | |
| (assoc out key)))) | |
| {} | |
| patterns)) | |
| (defn pull | |
| "Pulls given an entity." | |
| [patterns ent] | |
| (reduce | |
| (fn [out sub-pat] | |
| (if (map? sub-pat) | |
| (merge out (pull-map sub-pat ent)) | |
| (if-some [v (get ent sub-pat)] | |
| (assoc out sub-pat v) | |
| out))) | |
| {} | |
| patterns)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment