Last active
August 29, 2015 14:11
-
-
Save trygvea/123869aa641cadfca3f7 to your computer and use it in GitHub Desktop.
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 normalize | |
"Transforms a tabular form to a normalised hierarchical form." | |
([kw values-kw coll] | |
(normalize kw values-kw [] coll)) | |
([kw values-kw promoted-keys coll] | |
(normalize kw values-kw promoted-keys identity coll)) | |
([kw values-kw promoted-keys f coll] | |
(map | |
(fn [[k v]] | |
(merge {kw k | |
values-kw (f (map #(apply dissoc % (conj promoted-keys kw)) v))} | |
(select-keys (first v) promoted-keys))) | |
(group-by kw coll)))) | |
(def foo [{:a 1 :a2 9 :b 2 :b2 7 :c 3} | |
{:a 1 :a2 9 :b 2 :b2 7 :c 6} | |
{:a 1 :a2 9 :b 5 :b2 6 :c 7} | |
{:a 2 :a2 9 :b 8 :b2 5 :c 9}]) | |
(normalize :a :bs [:a2] (partial normalize :b :cs [:b2]) foo) | |
;; ({:a2 9, | |
;; :a 1, | |
;; :bs ({:b2 7, | |
;; :b 2, | |
;; :cs ({:c 3} | |
;; {:c 6})} | |
;; {:b2 6, | |
;; :b 5, | |
;; :cs ({:c 7})})} | |
;; {:a2 9, | |
;; :a 2, | |
;; :bs ({:b2 5, | |
;; :b 8, | |
;; :cs ({:c 9})})}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment