基于特定键中的值在集合中复制映射

时间:2016-12-10 10:48:04

标签: clojure

所以我有这个:

[{:a ["x" "y"], :b "foo"}
 {:a ["x" "y" "z"], :b "bar"}]

想要这个:

[{:a "x", :b "foo"} 
 {:a "y", :b "foo"} 
 {:a "x", :b "bar"}
 {:a "y", :b "bar"}
 {:a "z", :b "bar"}]

我该怎么做?

2 个答案:

答案 0 :(得分:5)

for对于已知的嵌套级别非常好:

(for [x [{:a ["x" "y"], :b "foo"}
         {:a ["x" "y" "z"], :b "bar"}]
      a (:a x)] 
  (assoc x :a a))

答案 1 :(得分:3)

您可以使用mapcat

(def c [{:a ["x" "y"], :b "foo"}
        {:a ["x" "y" "z"], :b "bar"}])

(mapcat (fn [{:keys [a] :as m}] (map #(assoc m :a %) a)) c)