解析项目的Clojure地图 - >类别 - 向量分类列表
题
我有商品名称和向量的映射,vectors存储关键字符串项目所在的类别。我试图将此映射解析为一个耦合的打字员,然后可以通过类别显示它们。
我认为我需要做的是解析地图一次,以制作一组所有可能的类别和子类别。一旦我有的时候,我可以迭代并过滤来自主贴图的所有匹配以获取正确的键字符串。
如何从下面的地图到一组所有主要和子类别?一旦我有那样,如何通过键的值使用它来查询原始地图?
谢谢任何帮助!
(def ITEM-CATEGORIES
{ "thingy" [["CatergoryA" "SubcategoryA"]]
"thingy2" [["FFT"]]
"thingy3" [["Generators" "Chaotic"]]
"thingy4" [["Analysis" "Pitch"] ["MachineListening"]]
"thingy5" [["Multichannel" "Ambisonics"]]
}
.
在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
. 解决方案
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"]}
其他提示
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.
不隶属于 StackOverflow