I've seen some interesting responses, but here's my solution.
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] (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)))) #'user/update-counter user> (update-counter 4 example) -- output removed -- user> (update-counter 1 example) -- output removed -- user> (clojure.pprint/pprint @example) [{:email {"[email protected]" 2}, :id 1} {:email {"[email protected]" 1}, :id 2} {:email {"[email protected]" 2}, :id 3} {:email {"[email protected]" 3}, :id 4}] nil
12.06.2013, 11:19, "Yoshinori Kohyama" <[email protected]>:
> Solutions seem to depend on whether you have two or more mail addresses in a ':emails' section or not.
> If you have, you should identify the mail address of which you want to increment the value.
> Anyway, try below:
>
> (dosync
> (alter result
> (fn [v]
> (mapv second
> (update-in (into {} (map #(vector (:id %) %) v))
> [1 :emails "[email protected]"]
> inc)))))
>
> HTH,
> with regards,
> Yoshinori Kohyama
>
> --
> --
> 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
> 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.
-- --
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.
