Array extras functionality for iterators
Michael A. Smith
michael at smith-li.com
Sat Feb 4 13:06:12 PST 2012
Sorry for the resend. Meant to include the list.
I like this idea a lot! However, what would be the correct behavior of
a method like 'every' on an infinite generator?
-Michael A. Smith
On Fri, Feb 3, 2012 at 3:54 PM, Domenic Denicola
<domenic at domenicdenicola.com> wrote:
> ES5's existing array extras make working with arrays a joy.
> However, sometimes arrays are not the right tool for the job. Perhaps you want lazy evaluation semantics (generators). Or perhaps you want to communicate that the list is immutable (compare .NET's `IEnumerable<T>` or Java's `Iterable<T>`). ES Harmony seems to have the answer: iterators! Like `IEnumerable<T>` or `Iterable<T>`, they are the most basic primitive of iteration. Yay!
> But, if my `fetchAllProducts()` method returns an iterator, I don't get my array extras. Sad.
> This may be solvable in library-space, but the iterator proposal doesn't seem to have an Iterator.prototype I could extend. So we end up with unfortunate underscore-style wrappers:
> _.some(_.map(iterator, mapper), predicate)
> I propose adding the array extras to any iterator (in some way), such that we can have syntax similar to the following:
> iterator.map(mapper).some(predicate) // returns an iterator
> The methods I would like to see are:
> * every, filter, forEach, map, reduce, reduceRight, some
> * Optionally: join, toString
> * Controversially: sorted, reversed (non-mutating versions of sort and reverse)
> What do you think?
> es-discuss mailing list
> es-discuss at mozilla.org
More information about the es-discuss