Skip to content

Instantly share code, notes, and snippets.

@odyssomay
Created May 5, 2011 13:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odyssomay/957028 to your computer and use it in GitHub Desktop.
Save odyssomay/957028 to your computer and use it in GitHub Desktop.
Recreate hierarchy
;; This one is inadequate, kept for history, use the one in the second file.
(defn sort-hierarchy [coll]
(if (every? #(= (count %) 1) coll)
[:files (map first coll)]
(map (fn [x]
[(ffirst x) (sort-hierarchy (map rest x))]) (partition-by first coll))))
(defn hash-hierarchy [coll]
(if (= (first coll) :files)
(last coll)
(apply merge (map (fn [x] {(first x) (hash-hierarchy (last x))}) coll))))
(defn restore-hierarchy [coll]
(-> coll sort-hierarchy hash-hierarchy))
(defn restore-hierarchy [coll]
(let [files (map first (filter #(= (count %) 1) coll))
levels (remove #(= (count %) 1) coll)]
(merge
(if (empty? files) {} {:files files})
(apply merge
(map (fn [x]
{(ffirst x) (restore-hierarchy (map rest x))})
(partition-by first levels))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment