Here's the refactored version.
user> (def example
             (ref 
              [{:id 1 :email {"[email protected]" 1}}
               {:id 2 :email {"[email protected]" 1}}
               {:id 3 :email {"[email protected]" 2}}
               {:id 4 :email {"[email protected]" 2}}]))
#'user/example
user> 
(defn update-counter [id xs]
  (dosync
   (let [_ @xs
         at-after (drop-while #(not= id (:id %)) _)
         mod-key (-> at-after first :email keys first)
         location (- (count _) (count at-after))]
     (alter xs update-in [location :email mod-key] inc))))
#'user/update-counter
user> (dotimes [_ 3]
	     (update-counter 3 example))
nil
user> (clojure.pprint/pprint @example)
[{:email {"[email protected]" 1}, :id 1}
 {:email {"[email protected]" 1}, :id 2}
 {:email {"[email protected]" 5}, :id 3}
 {:email {"[email protected]" 2}, :id 4}]
nil
 
 
 
12.06.2013, 14:47, "Meikel Brandmeyer (kotarak)" <[email protected]>:
Hi,

Am Mittwoch, 12. Juni 2013 06:39:59 UTC+2 schrieb Kelker Ryan:
user> (defn update-counter [id xs]
        (let [at-after (drop-while #(not= id (:id %)) @xs)
              to-modify (-> at-after first :email)
              mod-key (-> to-modify keys first)
              location (let [_ (- (count @xs) (count at-after))]
                         (if-not (pos? _) 0 _))]
          (dosync
           (alter xs update-in [location :email mod-key] inc))))
 
You have to wrap all accesses to xs in the same dosync.
 
Kind regards
Meikel
 

--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply via email to