clojure - 如何在 Clojure 中更新引用映射中的记录?

标签 clojure

假设以下场景:

(defrecord Person [firstname lastname])
(def some-map (ref {}))

(dosync
  (alter some-map conj {1 (Person. "john" "doe")})
  (alter some-map conj {2 (Person. "jane" "jameson")}))

要将“joe”的名字更改为“nick”,我执行以下操作:

(dosync
  (alter some-map (fn [m]                   
                  (assoc m 1 
                       (assoc (m 1) :firstname "nick")))))

在 Clojure 中执行此操作的惯用方法是什么?

最佳答案

不需要使用 update-in,对于这种情况,assoc-in 正是您想要的。

(dosync (alter some-map assoc-in [1 :firstname] "nick"))

关于clojure - 如何在 Clojure 中更新引用映射中的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12007347/

相关文章:

clojure - some & (first (filter in clojure?

clojure - Clojure 中的前向声明有哪些限制?为什么我不能在这个例子中使用 comp ?

clojure:如何列出当前命名空间中可访问的所有名称?

eclipse - Eclipse中Clojure的代码格式化实用程序

java - Clojure REPL 未在 Windows 命令提示符下启动

emacs - 有没有办法在 nrepl 中进行历史搜索?

dictionary - clojure 过滤嵌套映射以根据内部映射值返回键

clojure - Sentry Clojure 集成,Sentry 未收到警报。网络/队列错误

clojure - 为什么这个 clojure 宏需要 `' ~?

clojure - 在 Clojure 中使用 ns-resolve 调用函数时是否存在性能损失?