That First Next Argument

Kevin Smith zenparsing at gmail.com
Wed Aug 20 07:41:30 PDT 2014


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

Thanks Andy.  Creating data sinks (or any general mapping, such as `echo`)
really *are* awkward using the current `x = yield y` model.

The most natural solution I was able to come up with uses a closure
variable to capture the argument to `next` (and `throw` and `return`) and
make it available to the generator:

https://gist.github.com/zenparsing/26b200543bb8ae0ca4df#file-async-io-4-js

This makes writing the "echo" iterator much more pleasant:

    const echo = DataSink(input => function*() {
        while (true) yield input.value;
    });

I'm still curious why we need to go through such exercises, though.  It
seems clear to me that this is a weakness of the current design, and would
be easily addressed with syntax.  Is there a back-story that I'm not aware
of?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140820/bff4e358/attachment-0001.html>


More information about the es-discuss mailing list