2

I have map of item names and vectors of vectors which store categories which the key string item are in. I am trying to parse this map into a couple hiccup defpartials which then can display them organized by category.

What I think I need to do is parse the map once to make a set of all possible categories and sub categories. Once I have that I can iterate that and filter all matches from the main map to get the proper key strings.

How can I go from the map below, to a set of all main and sub categories? Once I have that set, how do i use it query the original map by values not by key?

thanks for any help!

(def ITEM-CATEGORIES
 { "thingy"          [["CatergoryA" "SubcategoryA"]]
   "thingy2"         [["FFT"]]
   "thingy3"         [["Generators" "Chaotic"]]
   "thingy4"         [["Analysis" "Pitch"] ["MachineListening"]]
   "thingy5"         [["Multichannel" "Ambisonics"]]
 }

goal in sudo code

(generate-hiccup-partial (create-set-of-unique-categories ITEM-CATEGORIES) ITEM-CATEGORIES)
....
(defpartial generate-hiccup-partial
  [categories map]
   ;; hiccup code
   (in-each-sub/main-category-get-keys-by-value categories map))  ;; return a list of all keys with the same categories
animuson
  • 53,861
  • 28
  • 137
  • 147
Jon Rose
  • 1,457
  • 1
  • 15
  • 25
  • The definition of ITEM-CATEGORIES is missing a closing parenthesis at the end. I was going to fix it but SO will bounce a one-character edit. Can you change it? – user100464 Mar 01 '12 at 18:09

2 Answers2

1

When I find my self thinking about going up and down nested data structure my mind jumps to the zipper library you could take ITEM-CATECORIES and build a zipper of it then make any number of relations by 'zipping' up and down the tree.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
1

I do not know what a defpartial is, but this will transform that map:

(defn xform [ic]
  (reduce (fn [result [k [vs]]]
        (reduce (fn [r v]
              (assoc r v (cons k (r v)))))
            result vs))
      {} ic))

user=> (xform ITEM-CATEGORIES)
{"SubcategoryA" ["thingy"], "CatergoryA" ["thingy"], "Ambisonics" ["thingy5"],
 "Multichannel" ["thingy5"], "Pitch" ["thingy4"], "Analysis" ["thingy4"],
 "Chaotic" ["thingy3"], "Generators" ["thingy3"], "FFT" ["thingy2"]}
user100464
  • 17,331
  • 7
  • 32
  • 40