Yes, it would be. But it's "wildly unlikely" that you need to do it.
You don't say what you're using next-sine! for, but I'll imagine
you're doing something like printing it. Maybe your code looks like:

(dotimes [n num-iters]
  (let [s current-sine]
    (next-sine!)
    (println s)))

This can easily be done without mutation:

(defn sines []
   (cycle (map #(Math/sin %) (range 0 6.28 0.01))))

(doseq [s (take num-iters (sines))]
  (println s))

This avoids mutating anything, and is as lazy as you need, because
doseq won't hold onto the head of the (sines) call, and (sines) is in
fact not bound anywhere globally (unlike your sines constant).

If you need to start in the middle of the seq for some reason, you can
(take num-iters (drop n-drop (sines))).

On Oct 14, 1:59 pm, "clwham...@gmail.com" <clwham...@gmail.com> wrote:
> I originally thought of calling nth on the seq but isn't that pretty
> wildly inefficient?
>
> On Oct 14, 1:38 pm, Moritz Ulrich <ulrich.mor...@googlemail.com>
> wrote:
>
> > Are you sure you need to capture the state in next-sine? It's not very
> > clojure-ly to have functions with state. I would capture the state in
> > the caller as an integer and just use get or nth on the lazy seq.
> > If you want to stick to your impure function, please mark it with a !
> > at the end: next-sine!
>
> > On Thu, Oct 14, 2010 at 9:52 PM, clwham...@gmail.com
>
> > <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...
>
> > > --
> > > 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
>
> > --
> > Moritz Ulrich
> > Programmer, Student, Almost normal Guy
>
> >http://www.google.com/profiles/ulrich.moritz
> > BB5F086F-C798-41D5-B742-494C1E9677E8
>
>

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