for-of statement of sparse array
brendan at mozilla.org
Thu Jul 5 18:15:50 PDT 2012
Jason Orendorff wrote:
> On Thu, Jul 5, 2012 at 2:09 PM, Allen Wirfs-Brock<allen at wirfs-brock.com> wrote:
>>> Map may win at some point, who knows? It's not winning if one wants an array, numeric indexing, .length, the usual prototype methods.
>> We could consider also have "dense" interators available:
>> for (let v of array.denseValues) console.log(v);
> This makes sense to me, but most arrays are meant to be dense in
> practice. So perhaps it makes more sense to add methods specifically
> for sparse arrays, making developers type more characters in the rare
> case rather than the common case:
> for (let v of array.sparseValues()) ...;
> for (let [i, v] of array.sparseItems()) ...;
No, this is human-hostile. Programmers don't generally make holes on
purpose. You're right that accidental holes may mean bugs, but not
necessarily -- especially if skipped. It's unclear and (I agree) messy,
but given holes and 1JS compatibility, we can't really change the main
iteration facilities used predominantly on arrays, and also used
generically on all objects including arrays, to skip holes.
The JSFixed request for Object.prototype.forEach wants nothing like
for(i=0;i<a.length;i++), when used on an Array instance. It must skip
holes. This upholes the Array forEach (and all other extras)
hole-skipping. The deck is stacked against for(;;) iteration in my view.
But to get back to your suggestion, that does not mean programmers think
about sparse arrays usefully or specifically. They want generic
iteration; they do not want holes to be iterated. At least, based on my
experience and what I see others writing.
More information about the es-discuss