You people are terrific, thanks so much.

I basically went with what Michal and Konrad worked out:

(let [feature-stream (ref nil)]
        (defn init-features [stream]
                (dosync (ref-set feature-stream stream))
                'ready)
        (defn get-feature []
                (dosync
                        (let [f (first @feature-stream)]
                                (alter feature-stream rest)
                                f))))

Works like a charm.  Are the ref to nil (as initial value) and the
constructor-like "init-features" in the closure idiomatic to Clojure?
I realize I'm writing scheme here :)

jds

On Jan 28, 2: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)))
>
> Sincerely,
> Michal

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