I want to append a value to a list in a Clojure atom:
(def thing (atom {:queue '()}))
I know when it's not an atom, I can do this:
(concat '(1 2) '(3))
How can I translate that into a swap! command?
Note: I asked a similar question involving maps: Using swap to MERGE (append to) a nested map in a Clojure atom?
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)}
If you write your own fn then it should be side effect free because it may call several time.
(def thing (atom {:queue '()}))
(swap! thing (fn [c]
(update-in c [:queue] concat '(1 2 3 3))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With