On Thu, Feb 12, 2009 at 12:22 PM, Meikel Brandmeyer <m...@kotka.de> wrote:
> Hi,
>
> Am 12.02.2009 um 18:29 schrieb Mark Volkmann:
>
>> (def f-infinite-seq (map f (iterate inc 0))) ; values 0 through infinity
>>
>> (println "The first is" (first f-infinite-seq))
>> (println "The third is" (nth f-infinite-seq 2))
>
> The result is cached in f-infinite-seq. So as long as you hold
> onto the head of the seq, the value will be there, even if you
> don't use the result of the "first" call.
>
> The solution is probably to not hold onto the head.

Thanks! I knew about the issue with holding onto the head, but for
some reason didn't notice that I was doing that. I guess I thought
that "holding the head" meant putting the first item from the lazy
sequence into a variable, not putting the lazy sequence itself into a
variable.

> Either feed the infinite seq directly to the consumer, without storing
> it in a global Var or a local. Or use a factory function which
> returns a fresh seq, everytime you call it.
>
> (defn f-infinite-seq
>  []
>  (map f (iterate inc 0)))
>
> (println "The first is" (first (f-infinite-seq)))
> (println "The third is" (nth (f-infinite-seq) 2)))

I like this approach. Thanks for explaining that!

-- 
R. Mark Volkmann
Object Computing, Inc.

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