Replies: 26 comments 1 reply
-
You seem to have realized yourself that |
Beta Was this translation helpful? Give feedback.
-
Please, have a look at the Comparison of programming languages (array). |
Beta Was this translation helpful? Give feedback.
-
I was not asking "does any language allow you to index with negative numbers" because I know quite a few that do. I'm asking if there's any language where |
Beta Was this translation helpful? Give feedback.
-
@GHNewbiee would be great if you can elaborate, else it's not really actionable |
Beta Was this translation helpful? Give feedback.
This comment has been minimized.
This comment has been minimized.
-
I have the following table:
For each pair I would like the most natural (in a point of making sense) way to store the calculated numbers. For example:
Tia |
Beta Was this translation helpful? Give feedback.
-
I see what you mean. I think we can make an "interesting" example with that—it would not be an array but would be fun to demonstrate anyway. |
Beta Was this translation helpful? Give feedback.
-
@Josh-Cena Try to be an array, because the workaround being asked is on array and not for fun. |
Beta Was this translation helpful? Give feedback.
-
The idea is to proxify an array. Arrays are exotic objects that observe assignments to |
Beta Was this translation helpful? Give feedback.
-
Could be feasible for this to be implemented behind the scene as |
Beta Was this translation helpful? Give feedback.
-
Well, to a good measure, this isn't really a JavaScript-specific issue; it's applicable to other languages, or just generally CS/Mathematics.
You have an interval of integers from
Now, from the look of it, what you're performing seems similar to a well-known technique called "memoisation" (or "memoization").
Even here I would suggest addition/subtraction. Another idea is to use modulo, or even
And... it occurs to me that Mathematically, const { floor } = Math;
const modulo = (x, y) => x - y * floor(x / y);
result[modulo(-1, 3)] = f(-1);
result[modulo(0, 3)] = f(0);
result[modulo(1, 3)] = f(1); But again, in my opinion it seems out of place for MDN's documentation. |
Beta Was this translation helpful? Give feedback.
-
This seems odd / overkill, and quite ill-performant. If anything, I would suggest an aggregate structure that uses two arrays write(k, v) {
const array = [this.non_negatives, this.negatives][Number(k < 0)];
array[Math.abs(k)] = v;
} @GHNewbiee Why do you specifically need arrays? I believe more details are warranted... |
Beta Was this translation helpful? Give feedback.
-
Ah, just for clarity, backlinking from the discourse discussion as well: https://es.discourse.group/t/negative-indices-in-array/1418 I think what we can do on that page is:
|
Beta Was this translation helpful? Give feedback.
-
I just need the simplest and most natural way to work with.
This is misleading and contrivance. Either we write:
both indices In addition and the most important is that ES/JS mainly supports two types of numbers: Floating point numbers and Integer numbers. Integers and natural numbers (positive integers) are of same semantics. Due to unlucky initial design/decision (or in favor of convenience), Moreover, if you, in general, believe that index has a semantics when we use |
Beta Was this translation helpful? Give feedback.
-
It doesn't have semantics. When you do |
Beta Was this translation helpful? Give feedback.
-
I may be wrong, but my understanding is: Arrays are not maps. They are more like stacks and queues (they implement the methods to act as such), and lists (with the help of an external index or the But they are not maps for any value, even any |
Beta Was this translation helpful? Give feedback.
-
Yep—that's correct.
To be fair, those (where |
Beta Was this translation helpful? Give feedback.
-
Yes, complexity guarantees would be great. |
Beta Was this translation helpful? Give feedback.
-
Then why do we call them Indexed Collections and not just (Natural) Ordered Collections? Because you need a mechanism to read the ordered elements, you need an index which will be 1-1 correlated to each element! Surprise! The elements DO have a kind of slack correlation to the index. As you already have written,
Sparse arrays can be used to describe actual situations and problems.
Which one is sub-set of the other, including |
Beta Was this translation helpful? Give feedback.
-
"Indexed" means you can index-access them. Doesn't mean index has anything special in it—otherwise it becomes a keyed collection. It's not "just" an ordered collection, since that would be something like a stack/queue/linked list which can't be index-accessed. What you understand to be an array doesn't necessarily correspond to what it's designed for. When you figure out it doesn't do what you need, it means you are using the wrong data structure.
Sparse arrays are widely regarded to be a mistake; you don't get these in other languages. Arrays on a metaphysical level are always densely populated. What you want is a
They are all orthogonal. As I said above, |
Beta Was this translation helpful? Give feedback.
-
No necessarily! It means find out an easy way to overcome the design weakness of the data structure and make it works for you. |
Beta Was this translation helpful? Give feedback.
-
Sure, I've seen people using strings as arrays, using arrays as stacks... Not saying that doesn't work, but every data structure has its inherent properties and you'll just shoot yourself in the foot if you try to stretch it. |
Beta Was this translation helpful? Give feedback.
-
Unless there is an actual concrete proposal for enhancing our docs, I think we should close this issue. |
Beta Was this translation helpful? Give feedback.
-
I do think we should explain how it's a bad idea to assign semantics to the numeric indices, at the bare minimum. |
Beta Was this translation helpful? Give feedback.
-
Hey folks - as the length of this thread is very long, I'm going to convert it to a discussion. Once a clear task is defined from the conclusion, pls open an issue with the task itself - thanks! |
Beta Was this translation helpful? Give feedback.
-
@Rumyra I'm very confused. I thought we did reach some sort of agreement on what to do in mdn/content#19051 (comment) (well actually, everyone else was not really willing to make changes, but since we have an issue, I decided to elaborate on that anyway). But since it seems the team is not motivated overall to address this issue, I guess I personally would not work on it at the end of the day. |
Beta Was this translation helpful? Give feedback.
-
MDN URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
What specific section or headline is this issue about?
Notes
What information was incorrect, unhelpful, or incomplete?
Instructions how to overcome the need of using negative indices.
1st Example
I have the consecutive numbers:
x: -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
and a functionf(x)
.The easiest way to save the results of the function for each number is to use an array with indices the numbers themselves:
This is not consistent since
result.length
is6
and not11
.2nd Example
I have another group of consecutive numbers:
y: -2, -1, 0, 1, 2, 3
and a new functionf(x, y)
.Again, the easiest way to save the results of the functions is to use a 2-dim array with indices the numbers themselves:
What did you expect to see?
Some practical workarounds from a mathematical point of view, too, if possible.
Do you have any supporting links, references, or citations?
No response
Do you have anything more you want to share?
An object could be used to store the functions results:
1st Example
which is relatively easy but not elegant at all.
But for the
2nd Example
using similar keys is not ideal at all.
MDN metadata
Page report details
en-us/web/javascript/reference/global_objects/array
Beta Was this translation helpful? Give feedback.
All reactions