-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add Base.allunique #15914
Add Base.allunique #15914
Conversation
Allows testing if elements in a collection are distict (when compared with isequal). Terminates early in case of repeated elements, has methods for collections which are by construction distinct. Also added tests. See discussion at issue #15803.
The typo also hid the error in the function body that was not caught by the unit tests, now that is also fixed.
|
||
alldistinct(::Set) = true | ||
|
||
alldistinct(r::Range) = (length(r) == 1) || (step(r) > 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be:
(step(r) != 0) || (length(r) <= 1)
or potentially:
alldistinct{T}(r::Range{T}) = (step(r) !== zero(T)) || (length(r) <= one(T))
and add tests for negative step and zero length ranges.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Made the change, with tests.
Suggestion by @hayd. Add tests for negative ranges, and also empty collections.
@@ -141,6 +141,27 @@ function unique(f::Callable, C) | |||
out | |||
end | |||
|
|||
""" | |||
alldistinct(itr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs to be added to rst docs too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks for the reminder.
Is there anything preventing a merge? I understand the core devs are busy so it might take time, but in case I missed something that prevents a merge that I can fix please let me know. |
I think this is good. If no one has any more feedback, I'll merge soon. Note for next time, it can help in keeping things organized if you use a new branch (other than master of your fork) for making each pull request. |
""" | ||
alldistinct(itr) | ||
|
||
Test if all values from `itr` are distinct, when compared with `isequal`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, spoke too soon maybe - technically this isn't testing using isequal
until #9381 gets resolved, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?in
says that for sets, isequal is used
, so that looks OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Test if/Return true
if/
No comma before "when".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following discussion for issue #15803, this emerged as the preferred alternative.
Thanks. Can you squash the commits? |
Can't we start using githubs own squasher? |
Yes, PRs no longer have to squash commits manually, thank goodness. |
AppVeyor seems to be stuck, but it got to SUCCESS, so I'm calling it a pass. |
when did this get renamed to allunique? I thought alldistinct was clearer |
There was a whole conversation about it. |
There's a problem here which also popped up in #15009 , namely that you can have 'oversampled' ranges of floats: >> r = linspace(1.0, nextfloat(1.0), 10)
>> allunique(r)
true
>> allunique(collect(r))
false As suggested in #15009 (comment) you can prevent this with the following check: last(r) >= nextfloat(first(r), length(r) - 1) One more thing: This comparison length(r) <= one(T) seems strange to me. (I tried all this on 0.4.5, so apologies if Oh, also, isn't this a bit pointless: |
Allows testing if elements in a collection are distinct (when compared
with isequal). Terminates early in case of repeated elements, has
methods for collections which are by construction distinct. Also added
tests.
See discussion at issue #15803, which this PR fixes.