Hi,

On Jun 22, 4:55 am, Base <basselh...@gmail.com> 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}]]}
>                    {:data B4 :children [{:data C7} {:data C8}]]} ]

I have this function in use:

(defn table
  [reduce-fn initial data groups coll]
  (let [[group & groups] (seq groups)
        recursive        (if groups
                           (partial table reduce-fn initial data
groups)
                           (fn [m]
                             (->> m
                               (map #(get % data))
                               (reduce reduce-fn initial))))
        grouped          (seq-utils/group-by #(get % group) coll)]
    (into (sorted-map) (map (juxt key (comp recursive val))
grouped))))

And another helper:

(defn convert
  [coll]
  (map (fn [[k v]]
         (if (map? v)
           {:data k :children (convert v)}
           {:data k :children (map #(array-map :data %) v)}))
       coll))

And here a session:

user=> (table conj [] 2 [0 1]
              [[:A1 :B1 :C1]
               [:A1 :B1 :C2]
               [:A1 :B2 :C1]
               [:A1 :B2 :C2]
               [:A2 :B1 :C1]])
{:A1 {:B1 [:C1 :C2], :B2 [:C1 :C2]}, :A2 {:B1 [:C1]}}
user=> (convert *1)
({:data :A1,
  :children
  ({:data :B1, :children ({:data :C1} {:data :C2})}
   {:data :B2, :children ({:data :C1} {:data :C2})})}
 {:data :A2, :children ({:data :B1, :children ({:data :C1})})})

Hope this helps.

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to