Clojure flat sequence into tree -


i have following vector, [-1 1 2 -1 3 0 -1 2 -1 4 0 3 0 0]

which represents tree [[1 2 [3] [2 [4] 3]]]

where -1 begins new branch , 0 ends it. how can convert original vector usable tree-like clojure structure (nested vector, nested map)? think clojure.zip/zipper might i'm not sure how build function args.

zippers tool this: 

(require '[clojure.zip :as zip])  (def in [-1 1 2 -1 3 0 -1 2 -1 4 0 3 0 0]) (def out [[1 2 [3] [2 [4] 3]]])  (defn deepen [steps]   (->> steps        (reduce (fn [loc step]                  (case step                    -1 (-> loc                           (zip/append-child [])                           (zip/down)                           (zip/rightmost))                    0 (zip/up loc)                    (zip/append-child loc step)))          (zip/vector-zip []))        (zip/root)))  (assert (= (deepen in) out)) 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -