Iterator/Generator methods and Iterator Generics

TOYAMA Nao qv6n-tym at asahi-net.or.jp
Mon Jan 29 14:58:22 PST 2007


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;
};
Iterator.filter = function (iter, callback, thisObject) {
  return Iterator.prototype.filter.call(iter, callback, thisObject);
};

Of course "Iterator" in above code will be "Enumerator" in ES4 as per
the iterators and generators proposal [2].

[1] http://docs.python.org/lib/itertools-functions.html
[2]
http://developer.mozilla.org/es4/proposals/iterators_and_generators.html#enumeration



More information about the Es4-discuss mailing list