From 2799812d791df6fde45ab8315c0029fca616f920 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 23 Nov 2018 22:42:38 +0100 Subject: [PATCH 1/4] Fix show test when Sockets are loaded in main. (#30119) --- stdlib/Sockets/test/runtests.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/Sockets/test/runtests.jl b/stdlib/Sockets/test/runtests.jl index f6bd80aa7b3f5..6065debb2b8b7 100644 --- a/stdlib/Sockets/test/runtests.jl +++ b/stdlib/Sockets/test/runtests.jl @@ -66,7 +66,8 @@ end @test inet.port == 1024 io = IOBuffer() show(io, inet) - @test String(take!(io)) == "Sockets.InetAddr{Sockets.IPv4}(ip\"127.0.0.1\", 1024)" + str = "Sockets.InetAddr{$(isdefined(Main, :IPv4) ? "" : "Sockets.")IPv4}(ip\"127.0.0.1\", 1024)" + @test String(take!(io)) == str end @testset "InetAddr invalid port" begin @test_throws InexactError Sockets.InetAddr(IPv4(127,0,0,1), -1) From 43bc5c312a543eb349b8c29d226a954155b53c29 Mon Sep 17 00:00:00 2001 From: raghav9-97 Date: Sat, 24 Nov 2018 20:58:45 +0530 Subject: [PATCH 2/4] Added naive unique!(f, itr) function --- base/set.jl | 43 +++++++++++++++++++++++++++++++++++++++++++ test/sets.jl | 3 +++ 2 files changed, 46 insertions(+) diff --git a/base/set.jl b/base/set.jl index 1a25aa1c2a859..29709e7ee064f 100644 --- a/base/set.jl +++ b/base/set.jl @@ -178,6 +178,49 @@ function unique(f, C) out end +""" + unique!(f, A::AbstractVector) + +Selects one value from `A` for each unique value produced by `f` applied to +elements of `A` , then return the modified A. + +# Examples +```jldoctest +julia> unique!(x -> x^2, [1, -1, 3, -3, 4]) +3-element Array{Int64,1}: + 1 + 3 + 4 + +julia> unique!(n -> n%3, [5, 1, 8, 9, 3, 4, 10, 7, 2, 6]) +3-element Array{Int64,1}: + 5 + 1 + 9 + +julia> unique!(iseven, [2, 3, 5, 7, 9]) +2-element Array{Int64,1}: + 2 + 3 +``` +""" +function unique!(f, A::AbstractVector) + seen = Set{eltype(A)}() + idxs = eachindex(A) + y = iterate(idxs) + count = 0 + for x in A + t = f(x) + if t ∉ seen + push!(seen,t) + count += 1 + A[y[1]] = x + y = iterate(idxs, y[2]) + end + end + resize!(A, count) +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) diff --git a/test/sets.jl b/test/sets.jl index e9342439f0a2f..f55bac7c3eefb 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -398,6 +398,9 @@ end unique!(u) @test u == [5,"w","we","r"] u = [1,2,5,1,3,2] + @test unique!(x -> x ^ 2, [1, -1, 3, -3, 5, -5]) == [1, 3, 5] + @test unique!(n -> n % 3, [5, 1, 8, 9, 3, 4, 10, 7, 2, 6]) == [5, 1, 9] + @test unique!(iseven, [2, 3, 5, 7, 9]) == [2, 3] end @testset "allunique" begin From e562c06c018fa831830c9b8f29fb942daed76c60 Mon Sep 17 00:00:00 2001 From: raghav9-97 Date: Sun, 25 Nov 2018 14:10:33 +0530 Subject: [PATCH 3/4] Fixed Set type error --- base/set.jl | 2 +- test/sets.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/base/set.jl b/base/set.jl index 29709e7ee064f..026aed3cd516b 100644 --- a/base/set.jl +++ b/base/set.jl @@ -205,7 +205,7 @@ julia> unique!(iseven, [2, 3, 5, 7, 9]) ``` """ function unique!(f, A::AbstractVector) - seen = Set{eltype(A)}() + seen = Set() idxs = eachindex(A) y = iterate(idxs) count = 0 diff --git a/test/sets.jl b/test/sets.jl index f55bac7c3eefb..ebe9db8ba90d9 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -401,6 +401,7 @@ end @test unique!(x -> x ^ 2, [1, -1, 3, -3, 5, -5]) == [1, 3, 5] @test unique!(n -> n % 3, [5, 1, 8, 9, 3, 4, 10, 7, 2, 6]) == [5, 1, 9] @test unique!(iseven, [2, 3, 5, 7, 9]) == [2, 3] + @test unique!(x -> x % 2 == 0 ? :even : :odd, [1, 2, 3, 4, 2, 2, 1]) == [1, 2] end @testset "allunique" begin From c2535b3bbf3a056cf9bdacea5df21ff2a6aec24a Mon Sep 17 00:00:00 2001 From: raghav9-97 Date: Sun, 25 Nov 2018 21:00:31 +0530 Subject: [PATCH 4/4] Modified _unique!(A) function --- base/set.jl | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/base/set.jl b/base/set.jl index 026aed3cd516b..6afff54d7cf0a 100644 --- a/base/set.jl +++ b/base/set.jl @@ -223,21 +223,7 @@ 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) - seen = Set{eltype(A)}() - idxs = eachindex(A) - y = iterate(idxs) - count = 0 - for x in A - if x ∉ seen - push!(seen, x) - count += 1 - A[y[1]] = x - y = iterate(idxs, y[2]) - end - end - resize!(A, count) -end +_unique!(A::AbstractVector) = unique!(identity, A::AbstractVector) # If A is grouped, so that each unique element is in a contiguous group, then we only # need to keep track of one element at a time. We replace the elements of A with the