There was a stackoverflow question recently that requested a solution for a 
similar problem:

https://stackoverflow.com/questions/22409638/remove-first-item-from-clojure-vector-atom-and-return-it

One solution there is similar to this:

(defn swap*!
  "Like swap! but returns a vector of [old-value new-value]"
  [atom f & args]
  (loop [old-value @atom]
    (let [new-value (apply f old-value args)]
      (if (compare-and-set! atom old-value new-value)
        [old-value new-value]
        (recur @atom)))))

This will return the correct old-value and new-value which you can diff.

Another note: 

> (swap! state #(update-in % [:teams] make-team))

can be written more succinctly:

(swap! state update-in [:teams] make-team)

--Steve

On Mar 20, 2014, at 6:28 PM, Jakub Holy <jakub.h...@iterate.no> wrote:

> I have couple of times run into a situation where I want to update a state 
> map held in an atom
> and return the change, not the new value. I haven't found a good way to do it 
> so either I am missing
> something obvious or there are more idiomatic ways to achieve what I need. 
> Could you advise me?
> 
> A concrete example: In ma webapp I want to assign a unique random ID to each 
> user. Creating that ID is simple:
> 
> (def state (atom {:teams {}}))
> 
> ;; Remove already used IDs from a lazy seq of random IDs (=> unique), take 
> the 1st one
> (defn unique-rand-id [id-set]
>   (first (remove id-set (repeatedly #(rand-int Integer/MAX_VALUE))))))
> 
> ;; Add a new team with a unique random ID to the teams map
> (defn make-team [teams]
>   (let [id (unique-rand-id (set (keys teams)))]
>     (assoc teams id {})))
> 
> ;; Create a new team; TODO: How to get the new team's ID?!
> (swap! state #(update-in % [:teams] make-team))
> 
> So I can generate and remember a new unique random ID but there is no way to 
> find out
> what ID it was (I cannot just take diff of state before and after since other 
> threads could
> have also added new IDs to it in the meanwhile.)
> 
> Any advice is appreciated. Thank you!
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to