Skip to content
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

[Issue 28415] Add unique!(f, itr) #28737

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ function unique(f::Callable, C)
out
end

"""
unique!(f, itr)

In-place replace an array containing one value from `itr` for each unique value produced by `f`
applied to elements of `itr`.

# Examples
```jldoctest
julia> v = Vector([1, -1, 3, -3, 4, -4, 5, -5, 6, -6])
julia> unique!(x -> x^2, v)
julia> print(v)
[1, 3, 4, 5, 6]
```
"""
function unique!(f::Callable, C)
out = Vector{eltype(C)}()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should this out do?

seen = Set()
i = 1
while i <= length(C)
y = f(C[i])
if !in(y, seen)
push!(seen, y)
i += 1
else
splice!(C, i)
end
end
end

# If A is not grouped, then we will need to keep track of all of the elements that we have
# seen so far.
function _unique!(A::AbstractVector)
Expand Down
3 changes: 3 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ end
u = [1,1,3,2,1]
unique!(u)
@test u == [1,3,2]
u = [5, 1, 8, 9, 3, 4, 10, 7, 2, 6]
unique!(n -> n % 3, u)
@test u == [5, 1, 9]
@test unique!([]) == []
@test unique!(Float64[]) == Float64[]
u = [1,2,2,3,5,5]
Expand Down