简体   繁体   English

如何只更新Clojure地图中的几个键?

[英]how to update only a few keys in a map in clojure?

I have a map in which i want to update the values(which are strings) but i want to update only a few keys not all the keys. 我有一张地图,我想更新值(是字符串),但我只想更新几个键,而不是所有键。

I am new and have no idea how to do it. 我是新手,不知道该怎么做。

Is it possible to update only a few keys in a Clojure map keeping the rest same? 是否可以只更新Clojure映射中的几个键,而保持其余键不变?

update is a valid way to do it if you want to apply a function to your values. 如果要将函数应用于值,则update是执行此操作的有效方法。 otherwise, you could just assoc new values to the map 否则,您可以将新值关联到地图

(def example {:a "a" :b "b" :c "c"})
(assoc example
       :a "foo"
       :c "bar")
#=> {:a "foo" :b "b" :c "bar")

or update-in for nested data update-in嵌套数据

(def example {:data {:a "a" :b "b" :c "c"}})
(update-in example [:data] assoc 
           :a "foo" 
           :c "bar")
#=> {:data {:a "foo" :b "b" :c "bar"}}

The following will update only keys :a and :b : 以下将仅更新键:a:b

(def example {:a 1 :b 2 :c 3})
(-> example
    (update :a inc)
    (update :b inc))
;; {:a 2, :b 3, :c 3}

Trying to summarize comments and answers so far... 试图总结到目前为止的评论和答案...

There are several ways to update only some of the keys in your map. 有几种方法可以仅更新地图中的某些键。 Which one is the best depends on 最好的取决于哪一个

  • Is your data structure nested? 您的数据结构是否嵌套?
    • If nested, use assoc-in instead of assoc or update-in instead of update . 如果嵌套,请使用assoc-in代替assoc或使用update-in代替update
  • Do you compute the new values based on the old one? 您是否基于旧值计算新值?
    • If you need the old values use update and update-in over assoc and assoc-in . 如果需要旧值,请使用assocassoc-in updateupdate-in assoc-in
  • How many keys do you have and how do you have them? 您拥有多少把钥匙,以及如何拥有它们?
    • The functions assoc , assoc-in , update , and update-in all use recursion under the hood for more than one key. 函数assocassoc-inupdateupdate-in都在幕后使用了多个键的递归。 With many keys you may run into stack overflow exceptions. 使用许多键,您可能会遇到堆栈溢出异常。 The same is true for the notation using -> which rewrites your code into nested calls. 使用->的符号也是如此,它将代码重写为嵌套调用。
    • In such cases, use into or merge if you'd use assoc otherwise. 在这种情况下,如果不使用assoc ,请使用intomerge
    • Using into or merge would also be easier if you don't have a fixed set of keys to update but something which is computed at run-time. 如果您没有固定的键集来更新,而是在运行时计算出某些内容,则使用intomerge也将更加容易。
    • Note that into may be faster than merge as it uses a polymorphic reduce under the hood. 请注意, into可能比merge更快,因为它在后台使用了多态reduce
    • If you're computing new values based on the old ones, ie would use update otherwise, consider iterating over your map using reduce once and collect the new values. 如果您要基于旧值计算新值,即要使用update ,请考虑使用reduce一次遍历地图并收集新值。 This is more low-level but may avoid iterating twice depending on your scenario. 这是更底层的,但根据您的情况,可以避免重复两次。

Examples 例子

Please see (and upvote :-) other responses for examples with assoc , assoc-in , update , and update-in . 请参阅(并赞扬:-)有关assocassoc-inupdateupdate-in示例的其他响应。

(def sample-map {:a 1 :b 2 :c 3})
(def new-values {:b 22 :c 33})
(into sample-map new-values)
(merge sample-map new-values)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM