Search code examples
clojureclojurescript

Toggle boolean in hashmap


The following works but feels/seems wrong. Is there a better way?

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}

Solution

  • (let [a (atom {:v true})]
        (swap! a update-in [:v] not)
        @a)
    => {:v false}
    

    or you can use specter:

    (transform [ATOM :v] not a)