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.
base/set.jl
Outdated
|
|
||
| alldistinct(::Set) = true | ||
|
|
||
| alldistinct(r::Range) = (length(r) == 1) || (step(r) > 0) |
There was a problem hiding this comment.
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.
Thanks. Made the change, with tests.
Suggestion by @hayd. Add tests for negative ranges, and also empty collections.
base/set.jl
Outdated
| end | ||
|
|
||
| """ | ||
| alldistinct(itr) |
There was a problem hiding this comment.
needs to be added to rst docs too
There was a problem hiding this comment.
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. |
base/set.jl
Outdated
| """ | ||
| alldistinct(itr) | ||
|
|
||
| Test if all values from `itr` are distinct, when compared with `isequal`. |
There was a problem hiding this comment.
hm, spoke too soon maybe - technically this isn't testing using isequal until #9381 gets resolved, right?
There was a problem hiding this comment.
?in says that for sets, isequal is used, so that looks OK.
There was a problem hiding this comment.
s/Test if/Return true if/
No comma before "when".
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))
falseAs 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.