Thanks for looking this over! It's interesting how often I find myself
trying to use an Atom because I'm not coordinating changes multiple
things, just one, but the solution still requires using a Ref. I guess
it's wrong to think of what I'm doing as coordinating one value. I'm
really coordinating two, a map and a thing inside the map (the last
value added).

On Sun, Mar 22, 2009 at 6:29 AM, Meikel Brandmeyer <m...@kotka.de> wrote:
> Hi,
>
> Am 21.03.2009 um 23:26 schrieb Mark Volkmann:
>
>> I'm looking for a suggestion on how I can get new-struct.
>
> The other aspects aside, here an answer for this question.
>
> (defn get-new-struct
>  [the-new-map]
>  (the-new-map (last (keys the-new-map))))
>
> Or, to be sure,
>
> (defn get-new-struct
>  [the-new-map]
>  (the-new-map (apply max (keys the-new-map))))
>
> This is of course ugly and O(n) as the last indicates.
>
> You could however change your b to take the atom
> instead of the map.
>
> (defn b
>  [the-atom your-data]
>  (let [new-id (get-new-id @the-atom)
>        new-struct (make-new-struct new-id your-data)]
>    (swap! the-atom assoc new-id new-struct)
>    new-struct))
>
> But now the deref and the swap! are not coordinated
> anymore. So one needs a Ref or an architecture, where
> all other users are only readers of the atom and one
> thread does sequentially add the new structs. Then
> the value of the atom wouldn't change between the
> deref and swap!. This maybe suggests an agent instead
> of an atom, but then you cannot return the new struct.
> And we would be back with the O(n) thing.
>
> So I agree with Laurent that you want a Ref here.
>
> Sincerely
> Meikel

-- 
R. Mark Volkmann
Object Computing, Inc.

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