-
Notifications
You must be signed in to change notification settings - Fork 13
Should lastIndex
return 0 if there are no items in the array?
#21
Comments
I would suggest returning Mathematical operations with It's downside is that one should not use loose comparsion in |
I just read the proposal and I came to raise the same issue. I think most people would expect Also, as far as mathematical conventions go, the last index of an empty list is often defined as |
Came here also to raise the same concern. As a teacher and author on JS, I am quite confident that having Unfortunately, Otherwise, we'll often have to combine with checks of
This
|
These desirable identities suggest
New learners should be taught
|
It's obviously mathematically convenient in some cases, but not in others: // run backwards through the array for some reason
for (let i = arr.lastIndex; i >= 0; i--) {
// ..
}
// oops, will run forever if array is empty [Edit: this example was a mistake. I intended different logic but typed it out too quickly.] "These identities require..." Nah, nothing is required here. In math, there's all sorts of examples where identities hold except for special base condition definitions, like "0!" and such. When talking about an operation that is only validly definable for an array that has contents, an empty array is exactly the sort of special base condition that deserves/supports a special case exception. I agree that making it For a "find" algorithm, there's a valid definition for finding something (or not!) in an empty array: we don't need to do any work in that case, and For I realize it's highly likely my argument will be rejected here. I just think we should consider it instead of dismissing the idea outright because it's less familiar. |
I don't follow why I take your point regarding "These identities require..." and have edited the wording.
Actually, one motivation for defining [off topic]
For my part, I believe the aim is to raise and explore different perspectives, ideas, & approaches; and — perhaps only sometimes — to reach consensus. I don't think anyone would dismiss a valid idea outright and I certainly gave your well reasoned idea consideration while writing up my alternative point of view. |
You're right, my bad, I was writing quickly and messed up the example logic. |
@getify I think Even we don't consider type theory, in practice, return Though I believe So personally I would also accept return |
The spec for
If
Then the
var empty = []; // length is 0 and lastIndex is -1, which is the same as (length - 1)
empty.slice( empty.lastIndex ); // another empty array
empty.fill( "-", empty.lastIndex ); // no-op
empty.includes( "-", empty.lastIndex ); // false
empty.indexOf( "-", empty.lastIndex ); // -1
empty.lastIndexOf( "-", empty.lastIndex ); // -1 |
Personally I find using indexOf somewhat annoying because of -1 in case nothing found. So |
@doasync , sure since -1 is already used a lot, it of course makes some sense to stick to it. But it's a very poor design in the first place. Forget for a minute all your great programmer knowledge and just read as plain english based on common human knowledge, which one is more obvious and reveals whats going on: In Julia for example there is OffsetArray in which indexes do not start from 0, and -1 is a perfectly valid index there. And it's also much more logical to expect index to be a valid index, or undefined, than to endow one arbitrary number to be more divine than others. |
I think you're discussing all this in the wrong place. This is an abandoned proposal; look at the README; it's been superseded by https://github.com/tc39/proposal-relative-indexing-method |
I'm wondering if it would be better to follow the same pattern as
indexOf
and return-1
forlastIndex
, when there are no items in the array. Returning0
would mean that it's not possible to distinguish an array with 0 and an array with 1 element.The text was updated successfully, but these errors were encountered: