Array extras functionality for iterators
domenic at domenicdenicola.com
Fri Feb 3 12:54:56 PST 2012
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?
More information about the es-discuss