Clojure:如何合并这两个地图?

发布于 2025-01-29 19:03:43 字数 331 浏览 2 评论 0原文

我有一个看起来像

{:a {:b {:c {:d [[1 2 3]]}
         :e "Hello"}}}

{:a {:b {:c {:c {:d [4 5 6]}}}}}}的地图。我如何合并这两个地图,以使结果看起来像这样?

{:a {:b {:c {:d [[1 2 3] [4 5 6]]}
         :e "Hello"}}}

I have one map that looks like

{:a {:b {:c {:d [[1 2 3]]}
         :e "Hello"}}}

and another map that looks like {:a {:b {:c {:d [[4 5 6]]}}}}. How can I merge these two maps so that the result looks like this?

{:a {:b {:c {:d [[1 2 3] [4 5 6]]}
         :e "Hello"}}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紅太極 2025-02-05 19:03:43

对于如此简单的用例,您可以选择坚持使用Core Clojure函数:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test))

(dotest
  (let [x    {:a {:b {:c {:d [[1 2 3]]}
                      :e "Hello"}}}
        y    {:a {:b {:c {:d [[4 5 6]]}}}}

        yseq (get-in y [:a :b :c :d])

        r1   (update-in x [:a :b :c :d] into yseq)

        r2   (update-in x [:a :b :c :d] #(into % yseq)) ]

        (is= r1 r2
          {:a {:b {:c {:d [[1 2 3]
                           [4 5 6]]},
                   :e "Hello"}}})))

R2所示,有时我认为使用独立的闭合函数以明确显示旧的位置是更清晰的值正在使用。我通常更加明确,将R2闭合写为:

(fn [d-val]
  (into d-val yseq))

而不是使用#(...)读取器宏。

For such a simple use-case, you might choose to stick with core Clojure functions:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test))

(dotest
  (let [x    {:a {:b {:c {:d [[1 2 3]]}
                      :e "Hello"}}}
        y    {:a {:b {:c {:d [[4 5 6]]}}}}

        yseq (get-in y [:a :b :c :d])

        r1   (update-in x [:a :b :c :d] into yseq)

        r2   (update-in x [:a :b :c :d] #(into % yseq)) ]

        (is= r1 r2
          {:a {:b {:c {:d [[1 2 3]
                           [4 5 6]]},
                   :e "Hello"}}})))

As shown for r2, I sometimes think it is clearer to use a self-contained closure function to explicitly show where the old value % is being used. I am often even more explicit, writing the r2 closure as:

(fn [d-val]
  (into d-val yseq))

instead of using the #(...) reader macro.

三生殊途 2025-02-05 19:03:43

您可以使用 deep-merge-with 来自不推荐的clojure-contrib.map-utils

(defn deep-merge-with [f & maps]
  (apply
    (fn m [& maps]
      (if (every? map? maps)
        (apply merge-with m maps)
        (apply f maps)))
    maps))

(def m1
  {:a {:b {:c {:d [[1 2 3]]}
           :e "Hello"}}})

(def m2
  {:a {:b {:c {:d [[4 5 6]]}}}})

(deep-merge-with into m1 m2)
;; => {:a {:b {:c {:d [[1 2 3] [4 5 6]]}
;;             :e "Hello"}}}

You can use deep-merge-with from the deprecated clojure-contrib.map-utils:

(defn deep-merge-with [f & maps]
  (apply
    (fn m [& maps]
      (if (every? map? maps)
        (apply merge-with m maps)
        (apply f maps)))
    maps))

(def m1
  {:a {:b {:c {:d [[1 2 3]]}
           :e "Hello"}}})

(def m2
  {:a {:b {:c {:d [[4 5 6]]}}}})

(deep-merge-with into m1 m2)
;; => {:a {:b {:c {:d [[1 2 3] [4 5 6]]}
;;             :e "Hello"}}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文