Domanda

Ho la mappa dei nomi degli articoli e i vettori dei vettori che memorizzano le categorie che memorizzano le categorie in cui si trovano la voce della stringa dei tasti. Sto cercando di analizzare questa mappa in un paio di imbroglioni sbirling che possono quindi visualizzarli organizzati per categoria.

Quello che penso di dover fare è analizzare la mappa una volta per creare una serie di tutte le possibili categorie e le sottocompresse categorie.Una volta che ho in grado di iterali e filtrare tutte le partite dalla mappa principale per ottenere le corrette stringhe chiave.

Come posso andare dalla mappa qui sotto, a un set di tutte le principali e le sottocompresse categorie?Una volta che ho impostato, come lo usi query IT la mappa originale per valori non chiave?

Grazie per qualsiasi aiuto!

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

Gol in codice sudo

(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
.

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top