Thank you Meikel, I was aware of the nil issue but it's good that you made that explicit.
I got rid of the 'symbol per your suggestion, and factored the closure to take the stream as an input: (defn init-features [stream] (let [feature-stream (ref stream)] (dosync (ref-set feature-stream stream)) (fn [] (dosync (let [f (first @feature-stream)] (alter feature-stream rest) f))))) It's amazing to me how concise this is. I'm really loving Clojure! I kinda miss typing 'lambda' instead of 'fn' or '#(', but I suppose a macro can fix that. Must have been done already :) On Jan 29, 1:42 am, Meikel Brandmeyer <m...@kotka.de> wrote: > Hi, > > On Jan 29, 5:05 am, free_variation <cane.c...@gmail.com> wrote: > > > > > 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 :) > > Please note, that you cannot distinguish a nil in the stream from an > exhausted stream. That's the reason I returned the whole seq in my > solution and not only the first element. Maybe this doesn't apply in > your case, but you should keep it in mind for other occasions. > > For the idiomatic question (and personal taste): Use :keywords instead > of 'quoted-symbols. I would not hard-wire the ref. I would make it > explicit as an argument. You can spice this more general function for > convenience with partial. > > Sincerely > Meikel -- 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