Created
November 17, 2023 12:41
-
-
Save kidd/b2cc4163b848b9dca87084b1b2caef73 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
(def hh [{:a 1, :b 2, :c 3 :aa 4} {:a 1, :b 2, :c 4 :aa 4} {:a 99, :b 2, :c 3 :aa 5}]) | |
(def rname :trial-ups) | |
(def main-f [:b :a]) | |
;; first stab, we pivot over the keys of the "root" entity, | |
;; group by them, and strip the rest out | |
(defn hydrate-with-main-entity-keys [m ks as] | |
(->> (group-by #(select-keys % main-f) hh) | |
(m/map-vals (partial map #(apply dissoc % main-f))) | |
(map (fn [[k v]] (assoc k rname (vec v)))))) | |
;; wait,... it would be good to be able to "strip" the child tables instead knowing which is the common part | |
(defn other-keys [m ks] | |
(keys (apply dissoc m ks))) | |
(defn hydrate-keys [m ks as] | |
(->> (group-by #(select-keys % (other-keys % ks)) m) | |
(m/map-vals (partial map #(apply dissoc % (other-keys % ks)))) | |
(map (fn [[k v]] (assoc k as (vec v)))))) | |
;; select-keys and dissoc are kind of opposite. instead of other-keys, we can just swap them | |
(defn hydrate-keys [m ks as] | |
(->> (group-by #(apply dissoc % ks) m) | |
(m/map-vals (partial map #(select-keys % ks))) | |
(map (fn [[k v]] (assoc k as (vec v)))))) | |
(hydrate-keys hh [:aa] :my-as) | |
(hydrate-keys | |
(hydrate-keys hh [:aa] :my-as) | |
[:c] | |
:my-cs) | |
(reduce #(apply hydrate-keys %1 %2) | |
hh | |
[[[:aa] :my-as] [[:c] :my-cs]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment