在哈希图中切换布尔值

Toggle boolean in hashmap

以下有效,但 feels/seems 错误。有没有更好的方法?

dev:cljs.user=> (def x (atom {:v true}))
#'cljs.user/x
dev:cljs.user=> (swap! x assoc-in [:v] (not (:v @x)))
{:v false}
dev:cljs.user=> (swap! x assoc-in [:v] (not (:v @x)))
{:v true}
(let [a (atom {:v true})]
    (swap! a update-in [:v] not)
    @a)
=> {:v false}

或者你可以使用幽灵:

(transform [ATOM :v] not a)
cljs.user=> (def x (atom {:v true}))
#'cljs.user/x
cljs.user=> (swap! x update :v not)
{:v false}
cljs.user=> @x
{:v false}