alter a map in a vector (reference)

2013-06-03 Thread Yinka Erinle
Hi, I struggling to find a way to alter a value in a map that resides in a vector. Given I have the following (def result (ref [ { :id 1 :emails { "yi...@mail.com" 1}} ] ) ) I like to alter the result in a transaction to become [{:id 1 :emails { "yi...@mail.com" 2}}]; increment the counter

Re: alter a map in a vector (reference)

2013-06-03 Thread Meikel Brandmeyer (kotarak)
Hi, Am Montag, 3. Juni 2013 22:16:54 UTC+2 schrieb Yinka Erinle: > > Given I have the following > (def result (ref [ { :id 1 :emails { "yi...@mail.com " 1}} ] > ) ) > > I like to alter the result in a transaction to become > [{:id 1 :emails { "yi...@mail.com " 2}}]; increment the > counter

Re: alter a map in a vector (reference)

2013-06-03 Thread Yinka Erinle
Thanks Meikel for the prompt reply, that does it. Please can you elaborate on how I can use the solution below to update a particular entry in the vector. [{:id 1 :email {"a...@mail.com 1}} {:id 2 :email {"d...@mail.com 1}} {:id 3 :email {"g...@mail.com 2}}] So I will like to write a functi

Re: alter a map in a vector (reference)

2013-06-03 Thread Alex Baranosky
You *can* update this inside the vector, but if you want to key by :id, I would personally recommend, you put the email data into a map and look it up by :id, like this: {1 {:id 1 :email {"a...@mail.com 1}} 2 {:id 2 :email {"d...@mail.com 1}} 3 {:id 3 :email {"g...@mail.com 2}}} Best, Alex O

Re: alter a map in a vector (reference)

2013-06-11 Thread Yoshinori Kohyama
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 {}

Re: alter a map in a vector (reference)

2013-06-11 Thread Kelker Ryan
I've seen some interesting responses, but here's my solution. user> (def example (ref [{:id 1 :email {"a...@mail.com" 1}} {:id 2 :email {"d...@mail.com" 1}} {:id 3 :email {"g...@mail.com" 2}} {:id 4 :email {"f...@mail.com" 2}}])) #'user/example user>

Re: alter a map in a vector (reference)

2013-06-11 Thread Meikel Brandmeyer (kotarak)
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 (

Re: alter a map in a vector (reference)

2013-06-11 Thread Kelker Ryan
Here's the refactored version.user> (def example (ref [{:id 1 :email {"a...@mail.com" 1}} {:id 2 :email {"d...@mail.com" 1}} {:id 3 :email {"g...@mail.com" 2}} {:id 4 :email {"f...@mail.com" 2}}])) #'user/example user> (defn

Re: alter a map in a vector (reference)

2013-06-12 Thread Laurent PETIT
Hello, A note about the code organization : I think you should strive to separate the code that does side effects from the code that works on the datastructures. This means you could refactor your refactored version as such: => (def example (ref [{:id 1 :email {"a...@mail.com" 1}}