本文介绍了Clojure:使用assoc-in的结果不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以使用的定义非常优美:

As an aside, the definition of assoc-in is quite graceful:

(defn assoc-in
  ;; metadata elided
  [m [k & ks] v]
  (if ks
    (assoc m k (assoc-in (get m k) ks v))
    (assoc m k v)))

如果您需要替换不能调用 assoc 的值,则需要改用更浅的层次并替换整个地图,而不只是值:

If you need to replace a value that assoc can't be called on you need to instead act on one level shallower and replace the whole map rather than just the value:

(assoc-in {:foo {:bar "hello"}} [:foo :bar] {:baz "world"})
;=> {:foo {:bar {:baz "world"}}}

如果其中包含其他值不想替换整个地图而丢失的地图,可以使用与 assoc :

If there are other values within the map that you don't want to lose by replacing the whole thing, you can use update-in with assoc:

(update-in {:foo {:bar "hello"}} [:foo] assoc :baz "hi")
;=> {:foo {:bar "hello", :baz "hi"}}

这篇关于Clojure:使用assoc-in的结果不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:44