Skip to content

Commit aa7ce21

Browse files
committed
add replace methods for Tuple
1 parent d90a2ac commit aa7ce21

File tree

2 files changed

+15
-52
lines changed

2 files changed

+15
-52
lines changed

base/set.jl

+9-52
Original file line numberDiff line numberDiff line change
@@ -731,70 +731,27 @@ end
731731

732732
### replace for tuples
733733

734-
@inline replace(f::Callable, t::Tuple{}, count::Int) = ()
735-
@inline replace(f::Callable, t::Tuple{Any}, count::Int) =
736-
count == 0 ? t : (f(t[1]),)
737-
738-
@inline replace(f::Callable, t::Tuple{Any,Any}, count::Int) =
739-
if count >= 2
740-
(f(t[1]), f(t[2]))
741-
elseif count == 0
742-
t
743-
elseif count == 1
744-
x = f(t[1])
745-
x === t[1] ?
746-
(x, f(t[2])) :
747-
(x, t[2])
748-
end
749-
750-
@inline replace(f::Callable, t::Tuple{Any,Any,Any}, count::Int) =
751-
if count >= 3
752-
(f(t[1]), f(t[2]), f(t[3]))
753-
elseif count == 0
754-
t
755-
else
756-
x = f(t[1])
757-
if x === t[1]
758-
if count == 1
759-
y = f(t[2])
760-
y === t[2] ?
761-
(x, y, f(t[3])) :
762-
(x, y, t[3])
763-
else # count == 2
764-
(x, f(t[2]), f(t[3]))
765-
end
766-
elseif count == 1
767-
(x, t[2], t[3])
768-
else
769-
y = f(t[2])
770-
y === t[2] ?
771-
(x, y, f(t[3])) :
772-
(x, y, t[3])
773-
end
774-
end
775-
776-
@inline replace(f::Callable, t::Tuple, count::Int) =
777-
if count >= length(t)
778-
map(f, t)
779-
elseif count == 0
734+
function _replace(f::Callable, t::Tuple, count::Int)
735+
if count == 0 || isempty(t)
780736
t
781737
else
782738
x = f(t[1])
783739
(x, _replace(f, tail(t), count - !==(x, t[1]))...)
784740
end
741+
end
785742

786-
@inline replace(f::Callable, t::Tuple; count::Integer=typemax(Int)) =
787-
replace(f, t, check_count(count))
743+
replace(f::Callable, t::Tuple; count::Integer=typemax(Int)) =
744+
_replace(f, t, check_count(count))
788745

789-
function replace(t::Tuple, count::Int, old_new::Tuple{Vararg{Pair}})
790-
@inline function new(x)
746+
function _replace(t::Tuple, count::Int, old_new::Tuple{Vararg{Pair}})
747+
_replace(t, count) do x
748+
@_inline_meta
791749
for o_n in old_new
792750
isequal(first(o_n), x) && return last(o_n)
793751
end
794752
return x
795753
end
796-
replace(new, t, count)
797754
end
798755

799756
replace(t::Tuple, old_new::Pair...; count::Integer=typemax(Int)) =
800-
replace(t, check_count(count), old_new)
757+
_replace(t, check_count(count), old_new)

test/sets.jl

+6
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,14 @@ end
586586
@testset "replace! & replace" begin
587587
a = [1, 2, 3, 1]
588588
@test replace(x -> iseven(x) ? 2x : x, a) == [1, 4, 3, 1]
589+
@test replace(x -> iseven(x) ? 2x : x, Tuple(a)) === (1, 4, 3, 1)
589590
@test replace!(x -> iseven(x) ? 2x : x, a) === a
590591
@test a == [1, 4, 3, 1]
591592
@test replace(a, 1=>0) == [0, 4, 3, 0]
593+
@test replace(Tuple(a), 1=>0) === (0, 4, 3, 0)
592594
for count = (1, 0x1, big(1))
593595
@test replace(a, 1=>0, count=count) == [0, 4, 3, 1]
596+
@test replace(Tuple(a), 1=>0, count=count) === (0, 4, 3, 1)
594597
end
595598
@test replace!(a, 1=>2) === a
596599
@test a == [2, 4, 3, 2]
@@ -615,6 +618,7 @@ end
615618

616619
for count = (0, 0x0, big(0)) # count == 0 --> no replacements
617620
@test replace([1, 2], 1=>0, 2=>0; count) == [1, 2]
621+
@test replace((1, 2), 1=>0, 2=>0; count) === (1, 2)
618622
for dict = (Dict(1=>2, 2=>3), IdDict(1=>2, 2=>3))
619623
@test replace(dict, (1=>2) => (1=>3); count) == dict
620624
end
@@ -668,7 +672,9 @@ end
668672

669673
# test that isequal is used
670674
@test replace([NaN, 1.0], NaN=>0.0) == [0.0, 1.0]
675+
@test replace((NaN, 1.0), NaN=>0.0) === (0.0, 1.0)
671676
@test replace([1, missing], missing=>0) == [1, 0]
677+
@test replace((1, missing), missing=>0) === (1, 0)
672678
end
673679

674680
@testset "⊆, ⊊, ⊈, ⊇, ⊋, ⊉, <, <=, issetequal" begin

0 commit comments

Comments
 (0)