You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Clojure中合并向量列表中的重复键?

解决方案

可以用Clojure的序列操作实现这个合并需求:

(def input '([:common/to "To" "client/pages/individual_profile.cljs"]
             [:common/to "To" "client/component/edit_experience_dialog.cljs"]
             [:search/advanced-size-b "6 - 50" "client/pages/search/search_conditions.cljs"]
             [:type "Type" "re_frame/signup.cljs"]
             [:type "Type" "re_frame/multiple_jurisdictions.cljs"]))

(defn merge-vectors [vecs]
  (->> vecs
       (group-by first)
       (map (fn [[key group]]
              (let [[_ label] (first group)
                    paths (map last group)]
                (into [key label] paths))))))

(merge-vectors input)

执行后输出结果:

([:common/to "To" "client/pages/individual_profile.cljs" "client/component/edit_experience_dialog.cljs"]
 [:search/advanced-size-b "6 - 50" "client/pages/search/search_conditions.cljs"]
 [:type "Type" "re_frame/signup.cljs" "re_frame/multiple_jurisdictions.cljs"])

逻辑说明

  • group-by first:将输入列表按向量首元素(:common/to这类键)分组,把同键向量归为一组。
  • 遍历每个分组时,从组内第一个向量提取键和对应标签,再收集组内所有向量的路径元素。
  • 用into把键、标签和所有路径拼接成新向量,完成合并。

内容的提问来源于stack exchange,提问作者Aleskert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 11:01:04