Question

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
Was it helpful?

Solution

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"]}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top