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.

Reply via email to