Re: Transform columnar data into a tree?

2010-06-23 Thread ka
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 r

Re: Transform columnar data into a tree?

2010-06-22 Thread Michał Marczyk
I think the following function does what you want (I've also published it @ http://gist.github.com/449266 if you find Gists prettier): (defn munge [vs] (if (== 1 (count (first vs))) (reduce into vs) (let [gs (group-by #(% 0) vs)] (map (fn [k v] {:data k :children v})

Re: Transform columnar data into a tree?

2010-06-22 Thread Base
Thank you all for your help. This helps so much! I did try using into a number of times, but didnt get that to work. I have to admit that, except for the rudamentary examples I have not used reduce too much yet. I was reviewing Stu Halloways code on group- by and was trying to come up with a wa

Re: Transform columnar data into a tree?

2010-06-22 Thread Justin Kramer
I took a crack at it. Whenever I think of building up a structure, I think of reduce or into. Like Meikel, I create a nested, sorted-map tree first, then convert to the vector-of-maps format you describe. (defn add-path [root path] (let [add-child (fn [parent child-path] (if

Re: Transform columnar data into a tree?

2010-06-22 Thread Meikel Brandmeyer
Hi, On Jun 22, 4:55 am, Base wrote: > [ >         {: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}]]} >          

Re: Transform columnar data into a tree?

2010-06-21 Thread David Nolen
Is it really necessary to have the keys :data and :children? If not: (reduce (fn [r x] (assoc-in r x {})) {} (partition 3 '(A1 B1 C1 A1 B1 C2 A1 B2 C3 A1 B2 C4 A2 B3 C5 A2 B3 C6 A2 B4 C7 A2 B4 C8))) {A2 {B4 {C8 {}, C7 {}}, B3 {C6 {}, C5 {}}}, A1 {B2 {C4 {}, C3 {}}, B1 {C2 {}, C1 { Works out

Transform columnar data into a tree?

2010-06-21 Thread Base
Hi all - This seems like it should be easy, but for some reason i have thought myself into a box on this and now am stuck. I have a data set of rows/ columns that has some hierarchical data in it: COLUMN A B C A1 B1 C1 A1 B1 C2 A1 B