Extending standard library of arrays

Brendan Eich brendan at mozilla.com
Sun Jul 10 16:03:37 PDT 2011


On Jul 10, 2011, at 3:51 PM, Rick Waldron wrote:

> As a sidenote, but with regard to forEach, can someone point me to some documentation that explains why the generic form of forEach doesn't work with objects (the use case has sufficient history)

You mean: why wasn't Array.prototype.forEach put on Object in the first place?

The array extras loop from 0 up to but not including this.length -- they're not enumerating properties. Enumeration of Array elements is unspecified and browsers behave differently (as for enumeration of indexed properties in non-Arrays, and of properties in general). Non-interoperation and no normative spec to appeal to, combined with the focus on Array not Object, kept forEach simple, and congruent to the other [0, this.length) indexing generics.

/be

> 
> Rick
> 
> 
> 
> -- Sent from my Palm Pre
> 
> On Jul 10, 2011 6:42 PM, David Bruant <david.bruant at labri.fr> wrote: 
> 
> Le 10/07/2011 22:46, Dmitry A. Soshnikov a écrit :
>> 
>> Here I put some extensions for arrays standard library (separated from this thread: https://mail.mozilla.org/pipermail/es-discuss/2011-July/015856.html where Array.of and Array.from were considered).
>> 
>> We can consider also the following (as a first step):
>> 
>> - Array.prototype.remove(value, all)
>> 
>> [1, 2, 3, 2].remove(2); // [1, 3, 2]
>> [1, 2, 3, 2].remove(2, true); // [1, 3]
>> 
>> (seems this function is required more than Array.of, because at least I saw it implemented in all frameworks and used it myself).
>> 
>> - Array.prototype.subtract(array)
>> 
>> [1, 2, 3, 4].subtract([2, 4]); // [1, 3]
>> 
>> - Array.seq(from, to) // or Array.range(from, to)
>> 
>> Array.seq(1, 5); // [1, 2, 3, 4, 5]
>> 
>> - Array.build(n, fn)
>> 
>> Array.build(5, function(index) index + 1); // [1, 2, 3, 4, 5]  
>> 
>> - Array.min(array), Array.max(array) (can be implemented with Math.max/min and apply though)
>> 
>> Array.min = (array) -> Math.min.apply(Math, array)
>> 
>> - Array.prototype.split(n)
>> 
>> ["a", "b", "c", "d", "e"].split(3) // [["a", "b", "c"], ["d", "e", "f"]]
>> 
>> Perhaps even to build objects from lists of keys and values (this function is usually called as `zip`):
>> 
>> - Object.fromArrays(["a", "b", "c"], [1, 2, 3]); // {a: 1, b: 2, c: 3}
>> 
>> - Array.prototype.unique
>> 
>> [1, 3, 2, 5, 5, 3].unique(); // [1, 3, 2, 5]
>> 
>> Thus, all names of methods can be discussed.
> I like a lot all of these ideas, but I can't help thinking that they do not seem to be aligned with the initial ECMAScript array design which is that arrays are ECMAScript objects (which is very different from what we'd understand of "array" in C or "lists" in Erlang as you cite them).
> The question I ask for each of your Array.prototype ideas is "how does it apply to non-dense arrays?".
> 
> Creating a List or a DenseArray (or both?) type sounds to better capture your intentions (especially since you provided a link to Erlang "list" methods). It could inherit everything from Array.prototype for free.
> Actually, this could be implemented with proxies :-)
> 
> Since we're suggesting array additions, I would be interested in trying to address one issue of forEach, map, every, some and filter.
> They all have a well-defined algorithm. Consequently, if the callback function has side-effects, these are deterministic. This, however, prevent efficient (parallelized, for instance) implementation. This is unfortunate since in a lot of cases, people don't do side-effect and would certainly trade the side-effect determinism guarantee for performance.
> Could it be considered to add non-deterministic versions of these functions? They would be defined like Array.prototype.sort is, in terms of guarantees (like "the callback function will be called at most once on which array element" for 'every' and 'some' for instance) rather than with an algorithm.
> I have no strong opinion on how to name them. Maybe adding an N (for "Non-deterministic") at the end of the equivalent method (Array.prototype.forEachN, Array.prototype.mapN, etc.)?
> 
> David
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110710/4b7d8a70/attachment.html>


More information about the es-discuss mailing list