Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created May 5, 2011 17:20
Show Gist options
  • Select an option

  • Save mwmitchell/957450 to your computer and use it in GitHub Desktop.

Select an option

Save mwmitchell/957450 to your computer and use it in GitHub Desktop.
reconstitution of de-normalized hashes (1 level deep only)
(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