Skip to content

Add Base.allunique #15914

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

Merged
merged 6 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ export
# collections
all!,
all,
allunique,
any!,
any,
collect,
Expand Down
21 changes: 21 additions & 0 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ function unique(f::Callable, C)
out
end

"""
allunique(itr)

Return `true` if all values from `itr` are distinct when compared with `isequal`.
"""
function allunique(C)
seen = Set{eltype(C)}()
for x in C
if in(x, seen)
return false
else
push!(seen, x)
end
end
true
end

allunique(::Set) = true

allunique{T}(r::Range{T}) = (step(r) != zero(T)) || (length(r) <= one(T))

function filter(f, s::Set)
u = similar(s)
for x in s
Expand Down
6 changes: 6 additions & 0 deletions doc/stdlib/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ Iterable Collections

Returns an array containing one value from ``itr`` for each unique value produced by ``f`` applied to elements of ``itr``\ .

.. function:: allunique(itr)

.. Docstring generated from Julia source

Return ``true`` if all values from ``itr`` are distinct when compared with ``isequal``\ .

.. function:: reduce(op, v0, itr)

.. Docstring generated from Julia source
Expand Down
14 changes: 14 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ u = unique([1,1,2])
@test unique(iseven, [5,1,8,9,3,4,10,7,2,6]) == [5,8]
@test unique(n->n % 3, [5,1,8,9,3,4,10,7,2,6]) == [5,1,9]

# allunique
@test allunique([])
@test allunique(Set())
@test allunique([1,2,3])
@test allunique([:a,:b,:c])
@test allunique(Set([1,2,3]))
@test !allunique([1,1,2])
@test !allunique([:a,:b,:c,:a])
@test allunique(4:7)
@test allunique(1:1)
@test allunique(4.0:0.3:7.0)
@test allunique(4:-1:5) # empty range
@test allunique(7:-1:1) # negative step

# filter
s = Set([1,2,3,4])
@test isequal(filter(isodd,s), Set([1,3]))
Expand Down