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

Preserve the input element type in unique #23208

Merged
merged 2 commits into from
Aug 24, 2017
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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ This section lists changes that do not have deprecation warnings.
way as `^(A::Integer, p::Integer)`. This means, for instance, that `[1 1; 0 1]^big(1)`
will return a `Matrix{BigInt}` instead of a `Matrix{Int}` ([#23366]).

* The element type of the input is now preserved in `unique`. Previously the element type
of the output was shrunk to fit the union of the type of each element in the input.
([#22696])

Library improvements
--------------------

Expand Down Expand Up @@ -1192,6 +1196,7 @@ Command-line option changes
[#22588]: https://github.com/JuliaLang/julia/issues/22588
[#22605]: https://github.com/JuliaLang/julia/issues/22605
[#22666]: https://github.com/JuliaLang/julia/issues/22666
[#22696]: https://github.com/JuliaLang/julia/issues/22696
[#22703]: https://github.com/JuliaLang/julia/issues/22703
[#22712]: https://github.com/JuliaLang/julia/issues/22712
[#22718]: https://github.com/JuliaLang/julia/issues/22718
Expand Down
10 changes: 8 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ const ⊆ = issubset

Return an array containing only the unique elements of collection `itr`,
as determined by [`isequal`](@ref), in the order that the first of each
set of equivalent elements originally appears.
set of equivalent elements originally appears. The element type of the
input is preserved.

# Examples
```jldoctest
Expand All @@ -249,6 +250,11 @@ julia> unique([1, 2, 6, 2])
1
2
6

julia> unique(Real[1, 1.0, 2])
2-element Array{Real,1}:
1
2
```
"""
function unique(itr)
Expand All @@ -260,7 +266,7 @@ function unique(itr)
return out
end
x, i = next(itr, i)
if !isleaftype(T)
if !isleaftype(T) && iteratoreltype(itr) == EltypeUnknown()
S = typeof(x)
return _unique_from(itr, S[x], Set{S}((x,)), i)
end
Expand Down
3 changes: 2 additions & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5382,7 +5382,8 @@ for U in unboxedunions
# deleteat!
F = Base.uniontypes(U)[2]
A = U[rand(F(1):F(len)) for i = 1:len]
deleteat!(A, map(Int, sort!(unique(A[1:4]))))
# The 2-arg `unique` method works around #22688
deleteat!(A, map(Int, sort!(unique(identity, A[1:4]))))
A = U[initvalue2(F2) for i = 1:len]
deleteat!(A, 1:2)
@test length(A) == len - 2
Expand Down
2 changes: 2 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ u = unique([1,1,2])
# issue 20105
@test @inferred(unique(x for x in 1:1)) == [1]
@test unique(x for x in Any[1,1.0])::Vector{Real} == [1]
@test unique(x for x in Real[1,1.0])::Vector{Real} == [1]
@test unique(Integer[1,1,2])::Vector{Integer} == [1,2]

# unique!
@testset "unique!" begin
Expand Down