On Tue, Nov 23, 2010 at 9:08 PM, Rick Moynihan <rick.moyni...@gmail.com> wrote:
> On 23 November 2010 19:01, Ken Wesson <kwess...@gmail.com> wrote:
>> On Tue, Nov 23, 2010 at 7:49 AM, Laurent PETIT <laurent.pe...@gmail.com> 
>> wrote:
>>> try
>>> (def x #(iterate inc 1))
>>> (take 1 (drop 1000000000 (x))
>>>
>>> if you do not want to blow up the memory.
>>
>> I wonder if an uncached lazy seq variant that cannot hold onto its
>> head would be useful to have in core?
>
> Such a thing would surely be a very dangerous structure to have around.
>
> Clojure's approach is one of immutability and minimal global state,
> and a non-cached lazy seq would either have to generate the whole
> sequence up to the last value required every time it was accessed, or
> it would be creating a  nasty implicit side effect in the var, as the
> seq would need to lose the new value each time it was accessed.

Obviously it would do the former, and would basically just be
syntactic sugar for the approach quoted above.

(def [seq-fn #(seq-generating-code)] -> (def uncached-seq
(nice-macro-name seq-generating-code))

(first-rest-map-or-whatever (seq-fn)) -> (first-rest-map-or-whatever
uncached-seq)

The implementation approach that occurred to me was to have a seqable
wrapper (some ISeq proxy say) around IFns, and the macro alluded to
above would simply wrap the seq-generating-code in (fn [] ...) and
wrap the function in one of those wrappers, so the whole expression
evaluated to something that implements ISeq but can be held onto
without holding onto its head.

The big question becomes: should the next method of the ISeq wrapper
produce another wrapper around a call to next on the underlying seq,
or just the seq produced by calling next on the underlying seq? In the
former case there's overhead on every step of every traversal, but you
can save e.g. (drop 5 my-uncached-seq) without holding onto ITS head;
in the latter case, hold onto (drop 5 my-uncached-seq) and you hold
onto its head but you can explicitly wrap with (nice-macro-name (drop
5 my-uncached-seq)) if you need to.

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