I'm sure this can be simplyfied:
(defn mlg [attrs data]
(if (empty? attrs)
[ (reduce + (map :mv data)) {:children data}]
(let [parts (group-by (first attrs) data)
subtrees (map (fn [[value data]]
[value (mlg (rest attrs) (map #(dissoc % (first
attrs)) data))])
parts)]
(reduce (fn [[sum tree] [value [sumsubtree subtree]]]
[(+ sum sumsubtree)
(update-in tree [:children] conj (assoc subtree
:path [(first
attrs) value]
:mv sumsubtree))]
)
[ 0.0 { :children [] }]
subtrees))))
Returns a pair with the sum for all items and a tree.
Each tree is represented as a dictionary, and inner nodes of the tree have
three keys:
- :mv the sum of :mv's of its children
- :path a pair of attr-value that represents all the leaves in the subtree
- :children the subtrees of this level
Leaves are represented as dictionaries with only the keys :sec_id and :mv.
I've forked your gist, so you can grab the code directly from
github https://gist.github.com/952861
Best regards,
Juan Manuel
--
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