On 28 Jan 2010, at 05:21, free_variation wrote:

I'm not clear on how to implement this without keeping a reference to
the sequence, say in a closure.  In scheme I might solve the problem
using a continuation.  But in clojure if I do, say:

(let [my-seq (....lazy-seq ...)]
   (defn get-some-data ...))

won't I run out of memory?  Is the solution to have my-seq somehow
refer to the next element to be retrieved, i.e. to the head of the
infinite sequence that has yet to be fed to the workers?

Your approach won't work for another reason: you can't change my-seq inside get-some-data, unlike in Scheme. Moreover, you have the problem of thread safety. A closure is not sufficient to protect the sequence against uncoordinated requests from multiple threads.

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))
#'user/get-some-data
user> (get-some-data)
1
user> (get-some-data)
2
user> (get-some-data)
3
user> (get-some-data)
1
user> (get-some-data)
2
user> (get-some-data)
3

Konrad.

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