Wanted: standard Array function to append an array's elements to another array

Jeff Walden jwalden+es at MIT.EDU
Fri Jul 29 15:10:23 PDT 2011


On 07/29/2011 05:22 AM, Andrea Giammarchi wrote:
> to avoid apply limits is actually trivial

More or less, yes.

But it requires the developer to anticipate the concern in advance that the elements being appended might consume all available stack space.  I don't think most developers think at all about the size of the stack, or about its being limited, except when they write a recursive algorithm, intentionally or inadvertently, and neglect to correctly implement the base case.  I certainly forgot about this concern when I wrote the buggy code which initially triggered this request, and I think it's reasonably apparent there's a problem when even a JS engine implementer makes this mistake.

Past that, your MAX_LENGTH constant would have to be lower than the max length across all JS engines cared about.  I find it concerning that something as simple as extending an array with the elements of another array would require an implementation-dependent workaround, when this operation is built-in functionality in other mainstream languages where mutation is common:

C++: vector<T>::insert
http://www.cplusplus.com/reference/stl/vector/insert/

C#: List<T>.AddRange
http://msdn.microsoft.com/en-us/library/z883w3dc.aspx#Y570

Java: List<E>.addAll
http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html

Perl: push
http://perlmeme.org/howtos/perlfunc/push_function.html

Python: list.extend
http://docs.python.org/tutorial/datastructures.html

Ruby: array.concat
http://www.ruby-doc.org/core/classes/Array.html#M000224

Jeff


More information about the es-discuss mailing list