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

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to