【问题标题】:Update-in nested map更新嵌套地图
【发布时间】:2013-08-20 02:11:12
【问题描述】:

我是 clojure 的新手,我已经盯着这个有一段时间了,我敢肯定有一些基本的东西我只是看不到。我想合并两组,但它们是嵌套的,例如:

(def foo {:b #{:test}})
(def bar {:a {:b #{:ab}} :c :d})

我试过了:

=>(update-in bar [:a :b] conj (:b foo) )
{:a {:b #{#{:test} :ab}}, :c :d}

我想这是有道理的,但我想要的是 {:a {:b #{:test :ab}}, :c :d}

我似乎无法从集合中取出 #{:test} 来组合它,或者在给定 update-in 语法的情况下正确访问 :b 作为集合。

非常感谢任何帮助。

【问题讨论】:

    标签: clojure


    【解决方案1】:

    您需要使用into 而不是conj

    (update-in bar [:a :b] into (:b foo))
    ;= {:a {:b #{:test :ab}}, :c :d}
    

    【讨论】:

    • clojure.set/union 也可以工作,尽管它可能具有不同的性能特征(取决于所涉及的集合的大小和具体类型)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多