New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow tuples to be compared with order operators (<, >, <=, >=) #216
Comments
What would it mean to sort Symbols? The concept of generically sorting values, and thus also sorting containers of values, was rejected by the committee in June as part of the https://github.com/hemanth/generic-comparison proposal. |
I would assume that |
That’s certainly a choice that can be made. How would you sort a Box / object? |
I would assume "sorting lexicographically" means that |
I’m skeptical about the utility of this with those behaviors, but if the champions want to adopt it, I’ll look forward to the presentation that makes the case. |
The utility is presumably in contexts where you have lists of numbers or strings. |
Sure, but that was the case for the comparison proposal as well, for arrays of numbers or strings - and the committee rejected it, so I’d expect either both or neither to be acceptable. |
That was not my impression from the discussion, which focused very heavily on generic comparison and the spaceship operator and did not, to my recollection, raise the possibility of adding a method which specifically did lexicographic comparison of arrays (i.e. one which deferred directly to < on the underlying values). I suspect the committee would have been much more receptive to such a proposal; at the very least, I would not take that discussion as strong evidence that it would be opposed to it. |
Interesting to hear your different take. Lexicographic comparison of arrays was the motivating example for the proposal, certainly. |
It does seem that one of the concerns in the notes is that Tuples being new don't suffer the legacy As an aside, the whole 3-way sort thing I've always found to be an awkward design of array e.g. With tuples it could be as simple as: // Sort by name, then by age
const sorted = people.sortBy(person => #[person.name, person.age]); But that would be a separate proposal. |
@Jamesernator can you confirm if the following example correctly represents what you are proposing here? #[1, 1] > #[0, 1]; // => true
#[1, 1] > #[1, 0]; // => true
#[1, 1] >= #[1, 1]; // => true
#[1, 1] > #[1, 1]; // => false
#[1] >= #[1, 1]; // => false
#[1] >= #[1, 0]; // => ???
#[1, NaN] >= #[1, NaN]; // => ??? The last examples seem problematic to me and would push me towards thinking you should resolve this in userland javascript with a comparison function. At this point I think the idea is worth discussing but I fear we will add something strange to the proposal: having a systematic way to deal with edge cases (I am sure I missed a bunch of them!) would maybe make this less strange... |
The second last one would be
Even if this is the case, could we at least make |
For the second to last one that makes perfect sense. For the last one, it seems like |
Just to be clear, I wasn't meaning that if the proposal doesn't support lexical comparison, then it shouldn't allow comparison (except i.e. Rather than adopting the footgun behaviour that Array has where I get that for the most part Tuples are symmetric with Arrays by design, but there are other array footguns that haven't been ported over to |
I think from reading the spec that throwing is the current behaviour, as as far as I can tell |
I'm not sure I understand the thinking behind this. This behavior seems rather unintuitive and goes against the common definition of "lexicographical order". Why does it need to differ from the corresponding string example? After all, the result should stay the same when you remove the common prefix, right? So |
I had the comparison around the wrong way, oops. I edited it to be the right way around. |
Hi, we discusses with the champion gorup and will probably keep the current behavior for the time being which is throwing: https://tc39.es/proposal-record-tuple/#sec-tonumber This gives the possibility to layer it on later in a follow-on proposal. |
Part of the design of Tuples is that code can be ported back and forth between Tuples and Arrays if it operates in the common subset (no mutation and no equality comparison). We have particular reasons for using ===, not a deepEquals function, for comparing R&T for equality, but I don't think those reasons apply to < for relational comparison. This proposal would expand the restriction to include no relational comparison. I would prefer to instead add lexicographical comparison in a way which also works on Arrays as well, e.g. a method or function, while steering clear from the parts that the committee objected to of the previous proposal (e.g. a new symbol-based protocol for a new <=> operator which can diverge from <). |
In order to focus on changes that are necessary to finish this proposal, we are closing issues related to follow-on proposals. You are free to continue the discussion here and reference it in other venues. |
If it's decided that tuples shouldn't be directly comparable with
That way, we still get the benefit of being able to compare tuples, which is useful for many algorithms, without creating the conceptual conflict between |
So far the design of Tuples has kept the set of methods to be a subset of the Array methods. I think this subset property should would be good to maintain. |
I would like to propose allowing for comparing tuples lexographically (e.g. similar to Python). This would allow simpler implementations for sorting by multiple keys.
This is useful both when sorting arrays, as well as for other applications that need to compare things by multiple keys.
For example a priority queue could use this to sort by an arbitrary priority rule of it's entries:
The text was updated successfully, but these errors were encountered: