On 14 Oct 2010, at 21:52, clwham...@gmail.com wrote:

I need a function that produces the 'next' value from a lazy-seq --
something like a Python generator. I imagine it would have to be some
sort of closure like:

(def next-sine
   (let [sines (atom (cycle (map sin (range 0 6.28 0.01))))]
       #(swap! sines rest)))

Is there a more idomatic way of doing this? I don't see a lot of use
of closures in clojure...

Closures are common in Clojure, but mostly they capture values rather than storage locations.

Could you tell us why you "need" a function that relies on mutable state? Clojure has lots of functions to make, transform, and use sequences in a functional style, and those are usually preferred.

If your need comes from the wish to do stream processing without passing the stream around explicitly among lots of functions, consider using monads to abstract away the stream argument:

        
http://github.com/clojure/clojure-contrib/blob/master/modules/stream-utils/src/main/clojure/clojure/contrib/stream_utils.clj

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