Hi! On Thu, Jan 28, 2010 at 8:39 AM, Michał Marczyk <michal.marc...@gmail.com> wrote: > 2010/1/28 Konrad Hinsen <konrad.hin...@fastmail.net>: >> The Clojure solution for your problem is an agent, which makes the access >> thread-safe: >> >> user> (def data-source (agent (cycle [1 2 3]))) >> #'user/data-source >> user> (defn get-some-data [] (let [v (first @data-source)] (send data-source >> rest) v)) > > Wouldn't this make it possible for two threads to obtain the same > value of (first @data-source), then send two rest messages to the > agent? > > A ref would not have this problem, though: > > (def data-source (ref the_sequence)) > (defn get-some-data [] (dosync (let [v (first @data-source)] (alter > data-source rest) v)))
Right, or you could also use an atom: (def data-source (atom (cons nil the_sequence))) (defn get-some-data [] (first (swap! data-source rest))) But this retains the last read value until next call to get-some-data. I recently realized that one can work around this problem: (def data-source (atom (cons nil the_sequence))) (defn get-some-data [] (let [s (swap! data-source rest)] (swap! data-source #(if (identical? s %) (cons nil (rest %)) %)) (first s))) This make me think that one could optimize Atom/swap by avoiding the CAS and the validation (but not the watches) when (identical? new-value old-value). Christophe -- 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