That First Next Argument

Andy Wingo wingo at igalia.com
Wed Aug 20 01:42:44 PDT 2014


On Tue 19 Aug 2014 08:48, Claude Pache <claude.pache at gmail.com> writes:

> Le 19 août 2014 à 06:47, Kevin Smith <zenparsing at gmail.com> a écrit :
>
>     It appears that the current state of affairs is that the argument
>     supplied to the first call of `next` on a newborn generator is
>     "ignored and inaccessibe".
>     
>     Is there any way that the generator function can have access to that
>     lost data?
>
> This can be worked around. Basically, ask the generator to advance to
> the first `yield` at instantiation, and retrieve the value of the
> "first" `next()` with that `yield`. For example:

This has the disadvantage of starting computation in the generator, of
course, before it has been asked for.

While this workaround might serve the purpose of "observables", for lazy
sequences it's not quite right.  It effectively turns an "even" sequence
into an "odd" one (in the sense of Wadler's paper[1]), as for iterables you
would have to stash the first return value away somewhere, so you end up
with iterators being a pair whose car is strict and whose cdr is lazy.

Andy

[1] "How to add laziness to a strict language, without even being odd"
    http://homepages.inf.ed.ac.uk/wadler/papers/lazyinstrict/lazyinstrict.ps


More information about the es-discuss mailing list