Hi fellow clojurers: I need help to group my data at multiple levels (currently 4) using a tree structure and perform aggregate calculations at each level.
I have the below clojure code to generate a list of test records for me (using the get-rec function). (def reg-cntry-list {"America" ["USA" "Canada" "Mexico" "Venezuala" "Brazil" "Argentina" "Cuba"] "Asia" ["India" "Pakistan" "Singapore" "China" "Japan" "Sri Lanka" "Malaysia"] "Europe" ["UK" "Germany" "France" "Italy" "Belgium" "Turkey" "Finland"] "Middle East" ["Saudi Arabia" "Bahrain" "UAE" "Kuwait" "Yemen" "Qatar" "Iraq"] "Africa" ["Libya" "Tanzania" "South Africa" "Kenya" "Ethiopia" "Morocco" "Zimbabwe"]}) (def sec-ind-list {"Basic Materials" ["Apparel" "Auto Part" "Building" "Packaged"] "Consumer Goods" ["Beveragess" "Cigarettes" "Drugs" "Newspapers"] "Financial" ["Life Insurance" "Banking" "Investment" "Funds"] "Healthcare" ["Home care" "Hospitals" "Plans" "Medical"] "Industrial" ["Chemicals" "Cleaning" "Machine" "Lumber"] "Services" ["Advertising" "Broadcasting" "Education" "Publishing"] "Technology" ["Biotechnology" "Computers" "Data Storage" "Electronics"] "Utilities" ["Farm Products" "Electric" "Gas" "Oil"]}) (defn get-rec [] (let [r (rand-nth (keys reg-cntry-list)) s (rand-nth (keys sec-ind-list))] {:sec_id (rand-int 1000) :attr1 r :attr2 (rand-nth (reg-cntry-list r)) :attr3 s :attr4 (rand-nth (sec-ind-list s)) :mv (rand 1000000) })) ;generate 50 random records (def data (take 50 (repeatedly get-rec))) Each record (map) has the following keys: :sec_id - security id :attr1 - attribute 1 of the security :attr2 - attribute 2 of the security :attr3 - attribute 3 of the security :attr4 - attribute 4 of the security :mv - market value of the security What I need is a tree like data structure that can group my data in four levels (based on attr1, attr2, attr3 and attr4) as follows and also store total market value (total mv) for each level: root (total mv of all recs) |____America (total mv for America) | |____USA (total mv for America/USA) | |____Financial (total mv for America/USA/Financial) | |___Banking (total mv for America/USA/Financial/Banking) | |____ :sec_id 1 :mv 889 |____ :sec_id 2 :mv 393 |___Funds (total mv for America/USA/Financial/Funds) | |____ :sec_id 3 :mv 33 |____ :sec_id 4 :mv 393 |____Technology | |___Electronics | |____ sec_id 5 :mv 93 |____ sec_id 6 :mv 29 |___Data Storage | |____ sec_id 7 :mv 389 |____ sec_id 8 :mv 93 |____Canada | |____Industrial | |___Machine | |____ sec_id 10 :mv 34 |____ sec_id 11 :mv 93 |___Lumber | |____ sec_id 12 :mv 93 |____ sec_id 13 :mv 93 |___Europe (total mv for Europe) | |____Germany | |____Financial | |___Banking | |____ sec_id 1 :mv 93 |____ sec_id 2 :mv 93 |____Technology | |___Electronics | |____ sec_id 5 :mv 93 |____ sec_id 6 :mv 93 |____France | |____Industrial | |___Lumber | |____ sec_id 12 :mv 93 |____ sec_id 13 :mv 93 I tried the group-by function but can't write it to get the nested data I want and to perform the aggregate computations at each level. I am learning Clojure using this exercise which is part of a bigger project that I am interested in re-writing using Clojure. Any help greatly appreciated. Thanks Shoeb -- 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