-
Notifications
You must be signed in to change notification settings - Fork 20
toReversed()
is not reversible on sparse arrays
#101
Comments
This is the standard behavior of all ES6+ array methods - holes considered as [,,].includes(undefined) // => true |
Right—but only those without functionally equivalent pre-ES6 counterparts. |
It was already discussed many times - for example, #8. |
Maybe I should have mentioned I did read that thread, but I sent this issue specifically because I don't think "the general policy that all new array features since ES6 do not need to handle holes" is a justified argument, considering it breaks consistency with its mutating counterpart, breaks user expectation of what the method does, and makes documentation of this method unnecessarily harder. (For example, for |
I mentioned that in #8 (comment) |
I noticed that as well, which is why I'm surprised why you seem to have changed your stance here ^^ And I think that concern was never addressed either. |
I agree that it's better to handle holes as you propose, but it's not a strict position - those methods have much other deterioration compared to the originals - for example, killed subclassing support and killed |
I was just about to say that I can make the same case against using PS. I think removing |
I don't think that "the mutating counterpart" is an argument since this proposal is based on R&T and |
I consider the prevailing belief, in the spec since ES2015 and in userland since long before, to be that whenever possible one should avoid ever having sparse arrays, and one should be overjoyed to encounter a method that fixes an array of its sparseness. Every element is swapped. A hole’s element is |
I agree that sparse arrays are suboptimal and should be avoided at all costs. I disagree that array methods should "autofix" them or conflate them with |
I can (with great regret) live with the fact that
toSorted()
eliminates empty slots (although I think symmetry with functionally similar APIs is much more charitable than symmetry by the time they were introduced—cfflatMap
,findLast
). However, the fact thattoReversed()
is not reversible bothers me.This makes its explanation unnecessarily harder, compared to just saying "every element is swapped", which automatically covers the empty slot case.
The text was updated successfully, but these errors were encountered: