Hi, Meikel
I didn't know there is a lower level operation. Thanks.
!paws is a tony name :)
> However, my suspicion is, that you don't clearly divide the state handling
> from the program logic. The peek should happen in the update function you
> pass to swap!. If this is not side-effect free, it means that you have to
> coordinate with other identities and resources. Then a ref or an agent might
> be indeed a better fit than the atom.
The best way might be sealing stuff in poll! function and using
polymorphism so that we can easily switch identity between ref and
atom.
(defprotocol Pollable
(poll! [x]))
(extend-protocol Pollable
clojure.lang.Ref
(poll! [r]
(dosync
(let [item (peek @r)]
(alter r pop)
item)))
clojure.lang.Atom
(poll! [a] (peek (!paws a pop))))
Maybe overengineering.
2011/11/10 Meikel Brandmeyer (kotarak) <[email protected]>:
> Hi,
>
> an atom can be sufficient, but you need to drop to a lower level.
>
> (defn !paws
> "Like swap! but returns the old value of the atom."
> [a f & args]
> (loop []
> (let [old-value @a
> new-value (apply f old-value args)]
> (if (compare-and-set! a old-value new-value)
> old-value
> (recur)))))
>
> (peek (!paws atom-with-queue pop))
>
> However, my suspicion is, that you don't clearly divide the state handling
> from the program logic. The peek should happen in the update function you
> pass to swap!. If this is not side-effect free, it means that you have to
> coordinate with other identities and resources. Then a ref or an agent might
> be indeed a better fit than the atom.
>
> Sincerely
> Meikel
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en