Created
May 5, 2011 17:20
-
-
Save mwmitchell/957450 to your computer and use it in GitHub Desktop.
reconstitution of de-normalized hashes (1 level deep only)
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
| (defn parse-de-normalized-list [items] | |
| (let [r #"^.+::.+$" | |
| reducer (fn [prod item] | |
| (if-not (re-find r (:code item)) | |
| (let [child-r (re-pattern (str "^" (:code item) "::.+$")) | |
| children (filter #(re-find child-r (:code %1)) items) | |
| new-item (merge item {:children (vec children)})] | |
| (merge prod new-item)) | |
| prod))] | |
| (reduce reducer [] items))) | |
| (def normalized (parse-de-normalized-list [ | |
| {:code "INT" :name "Internet"} | |
| {:code "BRK" :name "Breakfast"} | |
| {:code "BRK::FRBRK" :name "Complimentary"} | |
| {:code "POOL" :name "Pool"} | |
| {:code "POOL::OPOOL" :name "Outdoor"}])) | |
| (prn normalized) => [ | |
| {:children [], :code "INT", :name "Internet"} | |
| {:children [{:code "BRK::FRBRK", :name "Complimentary"}], | |
| :code "BRK", :name "Breakfast"} | |
| {:children [{:code "POOL::OPOOL", :name "Outdoor"}], | |
| :code "POOL", :name "Pool"}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment