On Tue, Mar 23, 2010 at 9:35 AM, WoodHacker <ramsa...@comcast.net> wrote:

> I understand how conj works.    But how do you add a value to a
> persistent vector?    You have to add the new item to the vector with
> (conj vector item), but how do you assign the return value to the
> persistent vector.   So far I have it working with a def  -- (def
> vector (conj vector item)) -- but I'm not sure this is 'pure'
> Clojure.    Is there a better way, functionally, or am I there?
> Functional programming is the best way to go, but very few programs
> can exist without mutable persistent data.
>
> Bill
>

conj _does_ let you add a value to a persistent vector. The question is how
do you save this value for future use. Don't use def. Use a mutable
reference type. One way:

(def my-vector (atom []))
(swap! my-vector conj 'foo)
@my-vector ; [foo]

Depending on your use case agents or refs might be better suited.

David

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

To unsubscribe from this group, send email to 
clojure+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.

Reply via email to