-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
[stdlib] Make comparison operator choices consistent #137
Conversation
Since all other comparisons in the `min` and `max` functions use the less-than operator, this line should use the same operator as well.
This actually changes the (undocumented) semantics of the function slightly. If you have multiple distinct values that nevertheless compare as equal, the old implementation will return the last value in nearly all cases*, but your new implementation will return the first value (unless the first and second arguments are equal, then it will return the second value). *The one case it won't is if you pass 3 or more values, where the 2nd and 3rd argument compare equal (and are the maximal of the arguments), then it will return the 2nd argument instead of the 3rd. |
Right. I remember someone making an argument that |
I see the point. This raises two questions:
|
I think we should definitely fix the odd behavior here so So this basically boils down to two considerations:
*There may be a slight benefit to starting from the beginning of the array instead of the end, as it's usually faster to iterate forward over memory than backwards, but that's probably negligible here. |
It is not subjective. As I said, there are interesting non-trivial mathematical properties (IIRC, noted by Stepanov) that justify it one way (and possibly differently for min and max, I don't remember). It would be good if someone did more research on that. |
Here's a list of papers by Alexander Stepanov. I skimmed through Fundamentals of Generic Programming (PDF), which describes the desire to have axioms and "concepts", and gives a couple of examples but does not include anything about
It seems to me that following the STL's behavior here seems like a pretty good choice. If you can find a Stepanov paper that actually describes a relevant axiom, I'd be interested in seeing that, but I suspect that if he does describe one, it probably results in picking the leftmost one. |
@kballard Found it: http://stackoverflow.com/a/26586746 |
Huh, so that actually does match my "feels like" intuition. More importantly, it throws out the "they should behave the same" argument, saying that they should definitely not. The following argument does seem pretty compelling though:
I'm convinced. We should adopt this behavior. |
+1 for adopting the behavior. |
[AFK] When used in sorting algorithms:
Should preserve identity and order if x and y are unordered w.r.t. one another |
#566 adds tests that show that we already implement that. |
Pretty print initializers.
Make generic API for binary operators
Update Swift 4.0 compatibility support for `IBAnimatable` project.
Since all other comparisons in the
min
andmax
functions use the less-than operator, this line should use the same operator as well.