我有两个这样的地图矢量:
1:
[{:id 54785214 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 1254561561 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 156156456 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 77897445 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 4848121 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 787897212 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 45478789 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 112315478 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 154847841 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 65698898 :field "fff" :attributes [{:id 1 :field "ddd"}]}]
2:
[{:id 547485214 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 12545561561 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 1561456456 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 778927445 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 4848121 :field "hhh" :attributes [{:id 1 :field "ddd"}]}
{:id 7817897212 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 455478789 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 1123615478 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 1541847841 :field "fff" :attributes [{:id 1 :field "ddd"}]}
{:id 656988898 :field "fff" :attributes [{:id 1 :field "ddd"}]}]
和一个函数来连接这两个向量与合并地图与相同的键(我选择:id):
(defn join-map-sets [set-a set-b by-key]
(map #(apply merge %) (vals (group-by by-key (concat set-a set-b)))))
将此函数应用于地图1和2后,结果不像源地图那样排序。预览结果的前3个地图:
({:id 154847841, :field "fff", :attributes [{:id 1, :field "ddd"}]}
{:id 1541847841, :field "fff", :attributes [{:id 1, :field "ddd"}]}
{:id 656988898, :field "fff", :attributes [{:id 1, :field "ddd"}]})
对于这个问题:如何修改我的函数以始终保留地图的顺序?就像concat一样,但是替换/合并重复项。
PS。抱歉丑陋的例子,但对于简单的例子,它没有显示问题。
答案 0 :(得分:0)
最简单的方法是使用distinct
:
(distinct (concat v1 v2))
将为您提供删除重复项的延迟序列。
(v1
和v2
是您的向量..)
count
将证明重复项被删除:
(count (distinct (concat v1 v2)))
顺便说一句,您的代码示例中没有重复的:id
,请仔细查看ID编号是否完全匹配。