Iterator/Generator methods and Iterator Generics

Brendan Eich brendan at mozilla.org
Tue Jan 30 13:54:04 PST 2007


On Jan 29, 2007, at 2:58 PM, TOYAMA Nao wrote:

> JS1.7 introduced iterators/generators from Python, but it misses
> itertools functions [1].  I suggest ES4 supports these functions as
> methods of iterators/generators and generic methods so that they  
> can be
> applied to custom generators.  For example, an implementation of
> Python's ifilter in JS1.7 can be like:
>
> var GeneratorPrototype = (function () { yield; })().__proto__;
> Iterator.prototype.filter = GeneratorPrototype.filter =
> function (callback, thisObject) {
>   for (var i in this)
>     if (callback.call(thisObject, i, this))
>       yield i;
> };

I was just working on this part of the proposal.

In theory it is easy to add standard library methods to the spec at  
this point if they can be expressed entirely in the new language.  In  
practice we are saying "no new features", especially to features that  
refract through the type system and runtime semantics.

However, I think the ML- and Haskell-inspired itertools from Python  
have no such effects, and should be considered for inclusion in the  
standard library specified by ES4, so we don't have to re-standardize  
them later after shipping them in Firefox N+1 without a spec ;-).

But I do not believe they should be fixed or prototype methods of  
nominal types (classes). Rather, they should simply be generic  
functions in the iterator namespace, (e.g. iterator::zip or use  
namespace iterator; ... zip(...)).

With Array and String, ES1-3 stipulated generic prototype methods,  
making for awkwardness when applying to other kinds of objects. For  
ES4, as in JS1.6+, we are reflecting these as static generic methods  
on their classes, simply using the classes as namespaces.  With the  
iterator namespace, we can do better and simply write functions.   
That's what zip, etc., are -- they are not methods.

Thanks for your timely mail.  Comments welcome.

/be



More information about the Es4-discuss mailing list