Dictionary 如何使用clojure中的函数替换嵌套映射中的多个值?

Dictionary 如何使用clojure中的函数替换嵌套映射中的多个值?,dictionary,clojure,immutability,Dictionary,Clojure,Immutability,我是Clojure的新手,我有一个嵌套映射,结构如下: {:players {"p1" {:id "p1" :deck [] :hand [{:name "Imp" :entity-type :card}] :minions [{:damage-taken 0

我是Clojure的新手,我有一个嵌套映射,结构如下:

{:players                       
    {"p1" 
        {:id      "p1"
         :deck    []
         :hand    [{:name        "Imp"
                    :entity-type :card}]
         :minions [{:damage-taken                0
                    :attacks-performed-this-turn 0
                    :entity-type                 :minion
                    :name                        "Imp"
                    :id                          "m1"
                    :position                    0
                    :owner-id                    "p1"}]
         :hero    
                   {:name         "Jaina Proudmoore"
                    :id           "h1"
                    :entity-type  :hero
                    :mana         0
                    :damage-taken 0}}
etc

如果我想用一个新的地图来代替英雄,一个有着相同的键但不同的值的地图,我该怎么做呢?我已经尝试将更新函数映射到hero的键上,但没有成功

这有两个常用函数,当您要更改一个值时,在中关联;当您要使用函数基于当前值更改值或要更改多个值时,在中更新:

user> (def players {:players                       
                    {"p1" 
                     {:id      "p1"
                      :deck    []
                      :hand    [{:name        "Imp"
                                 :entity-type :card}]
                      :minions [{:damage-taken                0
                                 :attacks-performed-this-turn 0
                                 :entity-type                 :minion
                                 :name                        "Imp"
                                 :id                          "m1"
                                 :position                    0
                                 :owner-id                    "p1"}]
                      :hero    
                      {:name         "Jaina Proudmoore"
                       :id           "h1"
                       :entity-type  :hero
                       :mana         0
                       :damage-taken 0}}}})
#'user/players
在本例中,
updatein
是一个很好的匹配,因为它允许您对嵌套集合执行任何操作。这里有一个例子,assoc在以前的值的基础上增加了一些新值,您也可以在这里添加映射键的函数

user> (-> (update-in players [:players "p1" :hero] 
                     #(assoc %
                             :name (str "long lost twin of " (:name %))
                             :id           "h2"
                             :entity-type  :antihero
                             :mana         (inc (:mana %))
                             :damage-taken (dec (:damage-taken %))))
          clojure.pprint/pprint)
{:players
 {"p1"
  {:id "p1",
   :deck [],
   :hand [{:name "Imp", :entity-type :card}],
   :minions
   [{:damage-taken 0,
     :attacks-performed-this-turn 0,
     :entity-type :minion,
     :name "Imp",
     :id "m1",
     :position 0,
     :owner-id "p1"}],
   :hero
   {:name "long lost twin of Jaina Proudmoore",
    :id "h2",
    :entity-type :antihero,
    :mana 1,
    :damage-taken -1}}}} 
user> 

Tl可能重复;DR:不要
映射
更新
。在中使用
update或在
中使用
assoc。这与上面的问题略有不同,因为这是关于如何更新多个键的问题,其中的答案没有显示执行此特定操作的有效方法。我将在这里编辑标题以使其有所不同clear@JaredSmith当前位置Arthur是对的,我应该把它弄清楚否则投票被撤回