On Fri, Jun 8, 2012 at 4:35 AM, Gabo <delmasc...@gmail.com> wrote:
> I'm actually working on a simple function which would replace the nth
> element of a vector.

(assoc v n e) ;; returns a new vector with the nth element of v replaced by e

Note that it does not change the original vector.

> (def ids-in-use (ref [1 2 3]))
> (defn update-vector [v tid]
> (assoc v tid 10))

update-vector will return a new vector with the 'tid'th element
replaced by 10, leaving the original v unchanged.

> (update-ids-in-use [2])

Is this meant to be (update-vector @ids-in-use 2) ?

That would return the current value of @ids-in-use with the 2nd
element replaced by 10, i.e., [1 2 10] and would not change what
ids-in-use referred to (still [1 2 3]).

Since it sounds like you want to mutate ids-in-use and you just need
to replace its value, the following might do what you want:

(def ids-in-use (atom [1 2 3]))
(defn update-vector [v tid] (assoc v tid 10))
(swap! ids-in-use update-vector 2)

The latter will (atomically) replace the value inside ids-in-use with
the result of (update-vector @ids-in-use 2)

(defn update-ids-in-use [tid] (swap! ids-in-use update-vector tid))
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

-- 
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

Reply via email to