我有一系列地图.
;; input
[{:country "MX", :video 12345, :customer "cid1"}
{:country "US", :video 12345, :customer "cid2"}
{:country "MX", :video 54321, :customer "cid1"}]
Run Code Online (Sandbox Code Playgroud)
我想将其转换为多图.我想生成.
;; output
{"cid1"
{:actions
[{:country "MX", :video 12345, :customer "cid1"}
{:country "MX", :video 12345, :customer "cid1"}]},
"cid2"
{:actions
[{:country "US", :video 12345, :customer "cid2"}]}}
Run Code Online (Sandbox Code Playgroud)
我觉得我应该用update-in.有些事情......我只是没有弄清楚究竟是什么some-fn-here样的,我认为其他人可能有同样的问题.
(defn add-mm-entry
[m e]
(update-in m [(:customer e)] some-fn-here))
(def output (reduce add-mm-entry {} input))
Run Code Online (Sandbox Code Playgroud)
我想在我工作的时候把它扔给社区.如果我走错了路,请告诉我.
如果我正确理解了意图,那么您将按以下方式进行分组:客户然后将操作向量包装到:actions.您可以使用clojure.core/group-by进行分组,然后映射(clojure.core/map)结果:
(def v [{:country "MX", :video 12345, :customer "cid1"}
{:country "US", :video 12345, :customer "cid2"}
{:country "MX", :video 54321, :customer "cid1"}])
(->> v
(group-by :customer)
(map (fn [[cid xs]] {cid {:actions xs}}))
(into {}))
Run Code Online (Sandbox Code Playgroud)