How to add to a nested list in a Clojure atom?

I want to add a value to a list in a Clojure atom:

(def thing (atom {:queue '()})) 

I know when this is not an atom, I can do this:

 (concat '(1 2) '(3)) 

How can I translate this into a swap! team?

Note. I asked a similar question with maps: Using swap to MERGE (attach to) a nested map in a Clojure atom?

+3
clojure
source share
2 answers
 user=> (def thing (atom {:queue '()})) #'user/thing user=> (swap! thing update-in [:queue] concat (list 1)) {:queue (1)} user=> (swap! thing update-in [:queue] concat (list 2)) {:queue (1 2)} user=> (swap! thing update-in [:queue] concat (list 3)) {:queue (1 2 3)} 
+6
source share

If you write your own fn, then it should be free from side effects, as it can trigger several times.

 (def thing (atom {:queue '()})) (swap! thing (fn [c] (update-in c [:queue] concat '(1 2 3 3)))) 
+2
source share

All Articles