Hi Trevor,

I hope I've understood your problem correctly.

You can modify nested structures using e.g. update-in:

  (let [k "user1" v 1234]
    (swap! user-queues update-in k conj v))

That's assuming that a user queue already exists in the map. If it doesn't,
you could do something like:

  (let [k "user1" v 1234]
    (swap! user-queues #(assoc % k (conj (get % k
clojure.lang.PersistentQueue/EMPTY) v))))

Regards,
Stuart

On 14 September 2011 11:10, Trevor <tcr1...@gmail.com> wrote:

> Thanks for the quick responses.
>
> I'll try to answer Andy's question: "How do you know, in advance, that
> it doesn't need to handle such concurrent changes?" ... and at the
> same time I will try to provide this example to Stuart, hoping I can
> see how using a map inside an atom might work:
>
> Let's say my users log into a web page and each user has a queue that
> does stuff for them. Since the users id is unique and each user can
> only be logged in from one session, when I use the user id as a key
> within a hash-map then I know *well-enough* there will not be any
> concurrent changes to that key value pair, particularly since enqueing
> means each change is actually an just addition to the stack. -- So I
> am picking queue's to make a point... -- Queue's are chosen over lists
> as they are both constant time and fast. Being atomic is great, but
> wouldn't making a copy of a queue and re-assembling it defeat the
> purpose of using it?
>
> So let's try this:
>
> > (def user-queues* (atom (hash-map)))
> #'project/user-queues*
>
> > (swap! user-queues* assoc "user1" clojure.lang.PersistentQueue/EMPTY)
> {"user1" #<PersistentQueue clojure.lang.PersistentQueue@0>}
>
> > (@user-queues* "user1")
> #<PersistentQueue clojure.lang.PersistentQueue@0>
>
> I would like to add an item to the users queue, but it seems when
> using an atom I can only swap in and out the value as opposed to
> modifying the value in-place.
>

> So let's start with just the basic atom'd queue:
>
> > (def q (atom clojure.lang.PersistentQueue/EMPTY))
> #'project/q
>
> > (swap! q conj (seconds))
> #<PersistentQueue clojure.lang.PersistentQueue@d3232253>
>
> > (apply list (swap! q conj (seconds)))
> (1315961557 1315961570)
>
> awesome.
>
> Now I want to store each users queue in the user-queues* hash-map. How
> would I do that while maintaining a real queue? My initial attempts
> always lead to reading the full queue into a list, then to conj and
> item on that list, then I have to remake a queue to then be stored
> back into the map via swap....so it's at that point I might as well
> not be using a queue - right?
>
> Certainly hash-maps with queue's would be a reasonable idea - right?.
>
> Thanks.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
> 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
>

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