Add elements to a vector

2014-09-16 Thread Bods
The slave has a bunch of attributes, one of which is the info pertaining to its master. I initialize a vector slavesinfo but in the doseq, when I try adding "x" using "conj", nothing happens. What am I doing wrong? (defn slave-stats [] (let [ computer (get slave-info "computer") slaves

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread Hussein B.
Nice! :) Thanks all for help. On Tuesday, June 17, 2014 9:00:06 PM UTC+2, Thomas Heller wrote: > > > > On Tuesday, June 17, 2014 8:16:36 PM UTC+2, Hussein B. wrote: >> >> Oh, this works >> >> (dosync (alter v #(update-in %1 [1] (fnil conj [ ]) %2) 33)) >> > > Not sure what %2 or 33 are doing the

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread Thomas Heller
On Tuesday, June 17, 2014 8:16:36 PM UTC+2, Hussein B. wrote: > > Oh, this works > > (dosync (alter v #(update-in %1 [1] (fnil conj [ ]) %2) 33)) > Not sure what %2 or 33 are doing there but you can skip the #() function, alter (just like update-in) uses apply internally so you can just use:

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread James Henderson
Ah, you got there first :) James On Tuesday, 17 June 2014 19:20:52 UTC+1, James Henderson wrote: > > 'alter' expects your anonymous function to take at least one argument (the > current value of the ref being altered), whereas you've passed a 0-arg > function - there's no mention of a '%' so Cl

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread James Henderson
'alter' expects your anonymous function to take at least one argument (the current value of the ref being altered), whereas you've passed a 0-arg function - there's no mention of a '%' so Clojure expands it to: (fn [] (update-in @v (fnil conj []))) Indeed, I suspect you want a 2-arg function

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread Hussein B.
Oh, this works (dosync (alter v #(update-in %1 [1] (fnil conj [ ]) %2) 33)) On Tuesday, June 17, 2014 8:05:10 PM UTC+2, Hussein B. wrote: > > Thanks, it works. > > In case, my initial map is a ref type > > (def m (ref { } )) > > Why this isn't working? > > (dosync > >(alter m #(update-in @v

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread Hussein B.
Thanks, it works. In case, my initial map is a ref type (def m (ref { } )) Why this isn't working? (dosync (alter m #(update-in @v [1] (fnil conj [ ])) 11)) On Tuesday, June 17, 2014 7:55:14 PM UTC+2, Mauricio Aldazosa wrote: > > For updating the value of a map given a key you can use upd

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread James Reeves
On 17 June 2014 18:54, Mauricio Aldazosa wrote: > > user> (update-in {} [1] (fnil conj []) 22) > {1 [22]} > Good use of the fnil function! It's one of those functions I always forget exists. - James -- You received this message because you are subscribed to the Google Groups "Clojure" group.

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread James Reeves
Something like this, perhaps: (assoc m k (conj (m k []) v)) Where m is the map, k is the key, and v is the new value you want to append. This works because (m k []) will default to [] if the key k isn't in the map m. Usually if a key cannot be found, it defaults to nil. If you don't care so

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread Mauricio Aldazosa
For updating the value of a map given a key you can use update-in: user> (update-in {1 [11]} [1] conj 22) {1 [11 22]} Now, to handle the case when the key is not present, you can use fnil: user> (update-in {} [1] (fnil conj []) 22) {1 [22]} ​ Cheers, Mauricio -- You received this message becau

Re: How to add elements to a vector that is the value of a map key?

2014-06-17 Thread Hussein B.
Please keep in mind that I'm starting from an empty map. So, I'm looking for an idiomatic Clojure code to achieve my purpose where if the map doesn't has that specific key, it will create an entry of that key and add the value to the newly created vector. And if the key exists, then add the val

How to add elements to a vector that is the value of a map key?

2014-06-17 Thread Hussein B.
Hi, This is a very basic question, so be patient please! :) I have an empty map where key is an integer and the value is a vector. How I can add an element to the vector of a specific key? For example: {1 [11]} Then {1 [ 11 22]} Thanks for help and time. -- You received this message becau