Created
May 5, 2011 13:34
-
-
Save odyssomay/957028 to your computer and use it in GitHub Desktop.
Recreate hierarchy
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
;; 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)) |
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 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