Re: sequence and iterables

On Fri, Nov 15, 2013 at 6:23 PM, Boris Zbarsky <bzbarsky@mit.edu> wrote:
> On 11/15/13 9:17 PM, Jonas Sicking wrote:
>> Can you give an example of when this is a problem. Snapshotting an
>> iterable to me simply means iterating the iterator all the way through
>> and remembering the data that it returned.
>
> Sure.
>
> It's just that no one normally working with iterables would do that.

That's not true.  Unless I'm explicitly using iterables as infinite
streams, I usually snapshot iterable arguments immediately, to ensure
that they're in the type of iterable that I want.

When I am using them as infinite streams, I use functions designed for
that, or use a lazy mapping operation.

>> This is generally something that we want to do synchronously from the
>> API before returning. Otherwise we need to defined exactly when the
>> iterator is read. I can't think of any existing APIs that deal with
>> array-like things and that doesn't iterate through the whole
>> array-like before returning.
>
>
> The typical way one would use an iterable is something like this:
>
>   function f(iterable) {
>     for (value of iterable) {
>       doStuff(value);
>     }
>   }
>
> whereas the snapshotting approach is more like:
>
>   function f(iterable) {
>     var snapshot = Array.from(iterable);
>     for (value of snapshot) {
>       doStuff(value);
>     }
>   }
>
> which is slightly odd to do as the default thing with iterables...

Slightly, when you're just going to run through the iterable
immediately.  It's normal if you're going to store the data for
whatever reason.

~TJ

Received on Saturday, 16 November 2013 02:29:15 UTC