How about this (using only map, reduce) -
user=> (def data1 [[:a1 :b1 :c1]
[:a1 :b1 :c2]
[:a1 :b2 :c3]
[:a1 :b2 :c4]
[:a2 :b3 :c5]
[:a2 :b3 :c6]
[:a2 :b4 :c7]
[:a2 :b4 :c8]])
user=> (pprint
(reduce
(fn f [tree row]
(if (seq row)
(let [r (first row)
[idx sub-tree] (some
(fn [[i v]] (if (= r (:data v)) [i
(:children v)] false))
(map vector (range) tree))
entry (if (next row)
{:data r :children (f (or sub-tree []) (rest
row))}
{:data r})]
(if idx
(assoc tree idx entry)
(conj tree entry)))))
[]
data1))
[{:data :a1,
:children
[{:data :b1, :children [{:data :c1} {:data :c2}]}
{:data :b2, :children [{:data :c3} {:data :c4}]}]}
{:data :a2,
:children
[{:data :b3, :children [{:data :c5} {:data :c6}]}
{:data :b4, :children [{:data :c7} {:data :c8}]}]}]
- Thanks
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en