Skip to content

Commit

Permalink
delete replace[!](pred, A, new)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jul 28, 2018
1 parent 31bf0b8 commit 769562b
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 52 deletions.
43 changes: 0 additions & 43 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,27 +362,6 @@ function replace_pairs!(res, A, count::Int, old_new::Tuple{Vararg{Pair}})
_replace!(new, res, A, count)
end

"""
replace!(pred::Function, A, new; [count::Integer])
Replace all occurrences `x` in collection `A` for which `pred(x)` is true
by `new`.
# Examples
```jldoctest
julia> A = [1, 2, 3, 1];
julia> replace!(isodd, A, 0, count=2)
4-element Array{Int64,1}:
0
2
0
1
```
"""
replace!(pred::Callable, A, new; count::Integer=typemax(Int)) =
replace!(x -> ifelse(pred(x), new, x), A, count=check_count(count))

"""
replace!(new::Function, A; [count::Integer])
Expand Down Expand Up @@ -473,28 +452,6 @@ end
subtract_singletontype(::Type{T}, x::Pair{K}, y::Pair...) where {T, K} =
subtract_singletontype(subtract_singletontype(T, y...), x)

"""
replace(pred::Function, A, new; [count::Integer])
Return a copy of collection `A` where all occurrences `x` for which
`pred(x)` is true are replaced by `new`.
If `count` is specified, then replace at most `count` occurrences in total.
# Examples
```jldoctest
julia> replace(isodd, [1, 2, 3, 1], 0, count=2)
4-element Array{Int64,1}:
0
2
0
1
```
"""
function replace(pred::Callable, A, new; count::Integer=typemax(Int))
T = promote_type(eltype(A), typeof(new))
_replace!(x -> ifelse(pred(x), new, x), _similar_or_copy(A, T), A, check_count(count))
end

"""
replace(new::Function, A; [count::Integer])
Expand Down
10 changes: 1 addition & 9 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,16 +518,12 @@ end
@test replace!(x->2x, a, count=0x2) == [4, 8, 3, 2]

d = Dict(1=>2, 3=>4)
@test replace(x->x.first > 2, d, 0=>0) == Dict(1=>2, 0=>0)
@test replace!(x -> x.first > 2 ? x.first=>2*x.second : x, d) === d
@test d == Dict(1=>2, 3=>8)
@test replace(d, (3=>8)=>(0=>0)) == Dict(1=>2, 0=>0)
@test replace!(d, (3=>8)=>(2=>2)) === d
@test d == Dict(1=>2, 2=>2)
for count = (1, 0x1, big(1))
@test replace(x->x.second == 2, d, 0=>0, count=count) in [Dict(1=>2, 0=>0),
Dict(2=>2, 0=>0)]
end

s = Set([1, 2, 3])
@test replace(x -> x > 1 ? 2x : x, s) == Set([1, 4, 6])
for count = (1, 0x1, big(1))
Expand All @@ -553,13 +549,9 @@ end
# test eltype promotion
x = @inferred replace([1, 2], 2=>2.5)
@test x == [1, 2.5] && x isa Vector{Float64}
x = @inferred replace(x -> x > 1, [1, 2], 2.5)
@test x == [1, 2.5] && x isa Vector{Float64}

x = @inferred replace([1, 2], 2=>missing)
@test isequal(x, [1, missing]) && x isa Vector{Union{Int, Missing}}
x = @inferred replace(x -> x > 1, [1, 2], missing)
@test isequal(x, [1, missing]) && x isa Vector{Union{Int, Missing}}

@test_broken @inferred replace([1, missing], missing=>2)
x = replace([1, missing], missing=>2)
Expand Down

0 comments on commit 769562b

Please sign in to comment.