Skip to content

Commit c2bf344

Browse files
committed
Drop compat code for dims and init kwargs from #518, #528, #590, #592, and #613
1 parent 89c1e3d commit c2bf344

File tree

4 files changed

+156
-238
lines changed

4 files changed

+156
-238
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,8 @@ Currently, the `@compat` macro supports the following syntaxes:
7272

7373
* `range` supporting `stop` as positional argument ([#28708]).
7474

75-
* `Compat.accumulate`, `Compat.accumulate!`, `Compat.all`, `Compat.any`,
76-
`Compat.cumprod`, `Compat.cumprod!`, `Compat.cumsum`, `Compat.cumsum!`,
77-
`Compat.findmax`, `Compat.findmin`, `Compat.mapreduce`, `Compat.maximum`,
78-
`Compat.minimum`, `Compat.prod`, `Compat.reduce`, `Compat.sort`,
79-
and `Compat.sum` with `dims` keyword argument ([#25989],[#26369]).
80-
81-
* `Compat.mapreduce` and `Compat.reduce` with `init` keyword argument ([#27711]).
82-
8375
* `selectdim` to obtain a view of an array with a specified index for a specified dimension ([#26009]).
8476

85-
* `Compat.cat` with `dims` as keyword argument ([#27163])
86-
8777
* Single-argument `permutedims(x)` for matrices and vectors ([#24839]).
8878

8979
* `fetch` for `Task`s ([#25940]).

src/Compat.jl

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -76,79 +76,6 @@ end
7676
end
7777
end
7878

79-
if VERSION < v"0.7.0-DEV.4064"
80-
for f in (:mean, :median, :var, :varm, :std, :cov, :cor)
81-
@eval import .Statistics: $f # compatibility with old Compat versions
82-
end
83-
for f in (:cumsum, :cumprod, :sum, :prod, :maximum, :minimum, :all, :any)
84-
@eval begin
85-
$f(a::AbstractArray; dims=nothing) =
86-
dims===nothing ? Base.$f(a) : Base.$f(a, dims)
87-
end
88-
end
89-
for f in (:sum, :prod, :maximum, :minimum, :all, :any, :accumulate)
90-
@eval begin
91-
$f(f, a::AbstractArray; dims=nothing) =
92-
dims===nothing ? Base.$f(f, a) : Base.$f(f, a, dims)
93-
end
94-
end
95-
for f in (:findmax, :findmin)
96-
@eval begin
97-
$f(a::AbstractVector; dims=nothing) =
98-
dims===nothing ? Base.$f(a) : Base.$f(a, dims)
99-
function $f(a::AbstractArray; dims=nothing)
100-
vs, inds = dims===nothing ? Base.$f(a) : Base.$f(a, dims)
101-
cis = CartesianIndices(a)
102-
return (vs, map(i -> cis[i], inds))
103-
end
104-
end
105-
end
106-
@eval sort(a::AbstractArray; dims=nothing, kwargs...) =
107-
dims===nothing ? Base.sort(a; kwargs...) : Base.sort(a, dims; kwargs...)
108-
for f in (:cumsum!, :cumprod!)
109-
@eval $f(out, a; dims=nothing) =
110-
dims===nothing ? Base.$f(out, a) : Base.$f(out, a, dims)
111-
end
112-
end
113-
if VERSION < v"0.7.0-DEV.4064"
114-
mapreduce(f, op, a::AbstractArray; dims=nothing, init=nothing) =
115-
init === nothing ? (dims===nothing ? Base.mapreduce(f, op, a) : Base.mapreducedim(f, op, a, dims)) :
116-
(dims===nothing ? Base.mapreduce(f, op, init, a) : Base.mapreducedim(f, op, a, dims, init))
117-
reduce(op, a::AbstractArray; dims=nothing, init=nothing) =
118-
init === nothing ? (dims===nothing ? Base.reduce(op, a) : Base.reducedim(op, a, dims)) :
119-
(dims===nothing ? Base.reduce(op, init, a) : Base.reducedim(op, a, dims, init))
120-
accumulate!(op, out, a; dims=nothing) =
121-
dims===nothing ? Base.accumulate!(op, out, a) : Base.accumulate!(op, out, a, dims)
122-
# kept for compatibility with early adopters
123-
mapreduce(f, op, v0, a::AbstractArray; dims=nothing) =
124-
mapreduce(f, op, a, dims=dims, init=v0)
125-
reduce(op, v0, a::AbstractArray; dims=nothing) =
126-
reduce(op, a, dims=dims, init=v0)
127-
elseif VERSION < v"0.7.0-beta.81" # julia#27711
128-
mapreduce(f, op, a::AbstractArray; dims=nothing, init=nothing) =
129-
init === nothing ? (dims===nothing ? Base.mapreduce(f, op, a) : Base.mapreduce(f, op, a, dims=dims)) :
130-
(dims===nothing ? Base.mapreduce(f, op, init, a) : Base.mapreduce(f, op, init, a, dims=dims))
131-
reduce(op, a::AbstractArray; dims=nothing, init=nothing) =
132-
init === nothing ? (dims===nothing ? Base.reduce(op, a) : Base.reduce(op, a, dims=dims)) :
133-
(dims===nothing ? Base.reduce(op, init, a) : Base.reduce(op, init, a, dims=dims))
134-
end
135-
if VERSION < v"0.7.0-beta.81" # julia#27711
136-
mapreduce(f, op, itr; init=nothing) =
137-
init === nothing ? Base.mapreduce(f, op, itr) : Base.mapreduce(f, op, init, itr)
138-
reduce(op, itr; init=nothing) =
139-
init === nothing ? Base.reduce(op, itr) : Base.reduce(op, init, itr)
140-
end
141-
if VERSION < v"0.7.0-DEV.4534"
142-
reverse(a::AbstractArray; dims=nothing) =
143-
dims===nothing ? Base.reverse(a) : Base.flipdim(a, dims)
144-
end
145-
if VERSION < v"0.7.0-DEV.4738"
146-
Base.squeeze(A; dims=error("squeeze: keyword argument dims not assigned")) = squeeze(A, dims)
147-
end
148-
if VERSION < v"0.7.0-DEV.5165" # julia#27163
149-
cat(X...; dims = throw(UndefKeywordError("cat: keyword argument dims not assigned"))) = Base.cat(dims, X...)
150-
end
151-
15279
if !isdefined(Base, :selectdim) # 0.7.0-DEV.3976
15380
export selectdim
15481
@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, Base.setindex(map(Base.Slice, axes(A)), i, d))

test/old.jl

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,3 +867,159 @@ end
867867
@test !islowercase('A')
868868
@test uppercasefirst("qwerty") == "Qwerty"
869869
@test lowercasefirst("Qwerty") == "qwerty"
870+
871+
# 0.7.0-DEV.4064
872+
# some tests are behind a version check below because Julia gave
873+
# the wrong result between 0.7.0-DEV.3262 and 0.7.0-DEV.4646
874+
# see https://github.com/JuliaLang/julia/issues/26488
875+
Issue26488 = VERSION < v"0.7.0-DEV.3262" || VERSION >= v"0.7.0-DEV.4646"
876+
@test Compat.Statistics.mean([1 2; 3 4]) == 2.5
877+
@test Compat.Statistics.mean([1 2; 3 4], dims=1) == [2 3]
878+
@test Compat.Statistics.mean([1 2; 3 4], dims=2) == hcat([1.5; 3.5])
879+
@test Compat.cumsum([1 2; 3 4], dims=1) == [1 2; 4 6]
880+
@test Compat.cumsum([1 2; 3 4], dims=2) == [1 3; 3 7]
881+
@test Compat.cumprod([1 2; 3 4], dims=1) == [1 2; 3 8]
882+
@test Compat.cumprod([1 2; 3 4], dims=2) == [1 2; 3 12]
883+
@test Compat.sum([1 2; 3 4]) == 10
884+
@test Compat.sum([1 2; 3 4], dims=1) == [4 6]
885+
@test Compat.sum([1 2; 3 4], dims=2) == hcat([3; 7])
886+
@test Compat.sum(x -> x+1, [1 2; 3 4]) == 14
887+
Issue26488 && @test Compat.sum(x -> x+1, [1 2; 3 4], dims=1) == [6 8]
888+
Issue26488 && @test Compat.sum(x -> x+1, [1 2; 3 4], dims=2) == hcat([5; 9])
889+
@test Compat.prod([1 2; 3 4]) == 24
890+
@test Compat.prod([1 2; 3 4], dims=1) == [3 8]
891+
@test Compat.prod([1 2; 3 4], dims=2) == hcat([2; 12])
892+
@test Compat.prod(x -> x+1, [1 2; 3 4]) == 120
893+
Issue26488 && @test Compat.prod(x -> x+1, [1 2; 3 4], dims=1) == [8 15]
894+
Issue26488 && @test Compat.prod(x -> x+1, [1 2; 3 4], dims=2) == hcat([6; 20])
895+
@test Compat.maximum([1 2; 3 4]) == 4
896+
@test Compat.maximum([1 2; 3 4], dims=1) == [3 4]
897+
@test Compat.maximum([1 2; 3 4], dims=2) == hcat([2; 4])
898+
@test Compat.maximum(x -> x+1, [1 2; 3 4]) == 5
899+
@test Compat.maximum(x -> x+1, [1 2; 3 4], dims=1) == [4 5]
900+
@test Compat.maximum(x -> x+1, [1 2; 3 4], dims=2) == hcat([3; 5])
901+
@test Compat.minimum([1 2; 3 4]) == 1
902+
@test Compat.minimum([1 2; 3 4], dims=1) == [1 2]
903+
@test Compat.minimum([1 2; 3 4], dims=2) == hcat([1; 3])
904+
@test Compat.minimum(x -> x+1, [1 2; 3 4]) == 2
905+
@test Compat.minimum(x -> x+1, [1 2; 3 4], dims=1) == [2 3]
906+
@test Compat.minimum(x -> x+1, [1 2; 3 4], dims=2) == hcat([2; 4])
907+
@test Compat.all([true false; true false]) == false
908+
@test Compat.all([true false; true false], dims=1) == [true false]
909+
@test Compat.all([true false; true false], dims=2) == hcat([false; false])
910+
@test Compat.all(isodd, [1 2; 3 4]) == false
911+
@test Compat.all(isodd, [1 2; 3 4], dims=1) == [true false]
912+
@test Compat.all(isodd, [1 2; 3 4], dims=2) == hcat([false; false])
913+
@test Compat.any([true false; true false]) == true
914+
@test Compat.any([true false; true false], dims=1) == [true false]
915+
@test Compat.any([true false; true false], dims=2) == hcat([true; true])
916+
@test Compat.any(isodd, [1 2; 3 4]) == true
917+
@test Compat.any(isodd, [1 2; 3 4], dims=1) == [true false]
918+
@test Compat.any(isodd, [1 2; 3 4], dims=2) == hcat([true; true])
919+
@test Compat.findmax([3, 2, 7, 4]) == (7, 3)
920+
@test Compat.findmax([3, 2, 7, 4], dims=1) == ([7], [3])
921+
@test Compat.findmax([1 2; 3 4], dims=1) == ([3 4], [CartesianIndex(2, 1) CartesianIndex(2, 2)])
922+
@test Compat.findmax([1 2; 3 4]) == (4, CartesianIndex(2, 2))
923+
@test Compat.findmax([1 2; 3 4], dims=1) == ([3 4], [CartesianIndex(2, 1) CartesianIndex(2, 2)])
924+
@test Compat.findmax([1 2; 3 4], dims=2) == (hcat([2; 4]), hcat([CartesianIndex(1, 2); CartesianIndex(2, 2)]))
925+
@test Compat.findmin([3, 2, 7, 4]) == (2, 2)
926+
@test Compat.findmin([3, 2, 7, 4], dims=1) == ([2], [2])
927+
@test Compat.findmin([1 2; 3 4]) == (1, CartesianIndex(1, 1))
928+
@test Compat.findmin([1 2; 3 4], dims=1) == ([1 2], [CartesianIndex(1, 1) CartesianIndex(1, 2)])
929+
@test Compat.findmin([1 2; 3 4], dims=2) == (hcat([1; 3]), hcat([CartesianIndex(1, 1); CartesianIndex(2, 1)]))
930+
if VERSION < v"0.7.0-DEV.5238"
931+
# Test these functions if their counterparts are defined in Base. In the future, this
932+
# will be dealt with in StatsBase
933+
@test Compat.Statistics.varm([1 2; 3 4], -1) == 18
934+
@test Compat.Statistics.varm([1 2; 3 4], [-1 -2], dims=1) == [20 52]
935+
@test Compat.Statistics.varm([1 2; 3 4], [-1, -2], dims=2) == hcat([13, 61])
936+
@test Compat.Statistics.var([1 2; 3 4]) == 5/3
937+
@test Compat.Statistics.var([1 2; 3 4], dims=1) == [2 2]
938+
@test Compat.Statistics.var([1 2; 3 4], dims=2) == hcat([0.5, 0.5])
939+
@test Compat.Statistics.var([1 2; 3 4], corrected=false) == 1.25
940+
@test Compat.Statistics.var([1 2; 3 4], corrected=false, dims=1) == [1 1]
941+
@test Compat.Statistics.var([1 2; 3 4], corrected=false, dims=2) == hcat([0.25, 0.25])
942+
@test Compat.Statistics.std([1 2; 3 4]) == sqrt(5/3)
943+
@test Compat.Statistics.std([1 2; 3 4], dims=1) == [sqrt(2) sqrt(2)]
944+
@test Compat.Statistics.std([1 2; 3 4], dims=2) == hcat([sqrt(0.5), sqrt(0.5)])
945+
@test Compat.Statistics.std([1 2; 3 4], corrected=false) == sqrt(1.25)
946+
@test Compat.Statistics.std([1 2; 3 4], corrected=false, dims=1) == [sqrt(1) sqrt(1)]
947+
@test Compat.Statistics.std([1 2; 3 4], corrected=false, dims=2) == hcat([sqrt(0.25), sqrt(0.25)])
948+
@test Compat.Statistics.cov([1 2; 3 4]) == [2 2; 2 2]
949+
@test Compat.Statistics.cov([1 2; 3 4], dims=1) == [2 2; 2 2]
950+
@test Compat.Statistics.cov([1 2; 3 4], dims=2) == [0.5 0.5; 0.5 0.5]
951+
@test Compat.Statistics.cov([1 2; 3 4], [4; 5]) == hcat([1; 1])
952+
@test Compat.Statistics.cov([1 2; 3 4], [4; 5], dims=1) == hcat([1; 1])
953+
@test Compat.Statistics.cov([1 2; 3 4], [4; 5], dims=2) == hcat([0.5; 0.5])
954+
@test Compat.Statistics.cov([1 2; 3 4], [4; 5], corrected=false) == hcat([0.5; 0.5])
955+
@test Compat.Statistics.cov([1 2; 3 4], [4; 5], corrected=false, dims=1) == hcat([0.5; 0.5])
956+
@test Compat.Statistics.cov([1 2; 3 4], [4; 5], corrected=false, dims=2) == hcat([0.25; 0.25])
957+
@test Compat.Statistics.cor([1 2; 3 4]) [1 1; 1 1]
958+
@test Compat.Statistics.cor([1 2; 3 4], dims=1) [1 1; 1 1]
959+
@test Compat.Statistics.cor([1 2; 3 4], dims=2) [1 1; 1 1]
960+
@test Compat.Statistics.cor([1 2; 3 4], [4; 5]) [1; 1]
961+
@test Compat.Statistics.cor([1 2; 3 4], [4; 5], dims=1) [1; 1]
962+
@test Compat.Statistics.cor([1 2; 3 4], [4; 5], dims=2) [1; 1]
963+
end
964+
@test Compat.Statistics.median([1 2; 3 4]) == 2.5
965+
@test Compat.Statistics.median([1 2; 3 4], dims=1) == [2 3]
966+
@test Compat.Statistics.median([1 2; 3 4], dims=2) == hcat([1.5; 3.5])
967+
@test Compat.mapreduce(string, *, [1 2; 3 4]) == "1324"
968+
Issue26488 && @test Compat.mapreduce(string, *, [1 2; 3 4], dims=1) == ["13" "24"]
969+
Issue26488 && @test Compat.mapreduce(string, *, [1 2; 3 4], dims=2) == hcat(["12", "34"])
970+
@test Compat.mapreduce(string, *, [1 2; 3 4], init="z") == "z1324"
971+
@test Compat.mapreduce(string, *, (1, 2, 3, 4), init="z") == "z1234"
972+
@test Compat.mapreduce(string, *, [1 2; 3 4], dims=1, init="z") == ["z13" "z24"]
973+
@test Compat.mapreduce(string, *, [1 2; 3 4], dims=2, init="z") == hcat(["z12", "z34"])
974+
@test Compat.reduce(*, [1 2; 3 4]) == 24
975+
@test Compat.reduce(*, [1 2; 3 4], dims=1) == [3 8]
976+
@test Compat.reduce(*, [1 2; 3 4], dims=2) == hcat([2, 12])
977+
@test Compat.reduce(*, [1 2; 3 4], init=10) == 240
978+
@test Compat.reduce(*, (1, 2, 3, 4), init=10) == 240
979+
@test Compat.reduce(*, [1 2; 3 4], dims=1, init=10) == [30 80]
980+
@test Compat.reduce(*, [1 2; 3 4], dims=2, init=10) == hcat([20, 120])
981+
@test Compat.sort([1, 2, 3, 4]) == [1, 2, 3, 4]
982+
@test Compat.sort([1 2; 3 4], dims=1) == [1 2; 3 4]
983+
@test Compat.sort([1 2; 3 4], dims=2) == [1 2; 3 4]
984+
@test Compat.sort([1, 2, 3, 4], rev=true) == [4, 3, 2, 1]
985+
@test Compat.sort([1 2; 3 4], rev=true, dims=1) == [3 4; 1 2]
986+
@test Compat.sort([1 2; 3 4], rev=true, dims=2) == [2 1; 4 3]
987+
@test Compat.accumulate(*, [1 2; 3 4], dims=1) == [1 2; 3 8]
988+
@test Compat.accumulate(*, [1 2; 3 4], dims=2) == [1 2; 3 12]
989+
@test Compat.cumsum([1 2; 3 4], dims=1) == [1 2; 4 6]
990+
@test Compat.cumsum([1 2; 3 4], dims=2) == [1 3; 3 7]
991+
@test Compat.cumprod([1 2; 3 4], dims=1) == [1 2; 3 8]
992+
@test Compat.cumprod([1 2; 3 4], dims=2) == [1 2; 3 12]
993+
let b = zeros(2,2)
994+
Compat.accumulate!(*, b, [1 2; 3 4], dims=1)
995+
@test b == [1 2; 3 8]
996+
Compat.accumulate!(*, b, [1 2; 3 4], dims=2)
997+
@test b == [1 2; 3 12]
998+
Compat.cumsum!(b, [1 2; 3 4], dims=1)
999+
@test b == [1 2; 4 6]
1000+
Compat.cumsum!(b, [1 2; 3 4], dims=2)
1001+
@test b == [1 3; 3 7]
1002+
Compat.cumprod!(b, [1 2; 3 4], dims=1)
1003+
@test b == [1 2; 3 8]
1004+
Compat.cumprod!(b, [1 2; 3 4], dims=2)
1005+
@test b == [1 2; 3 12]
1006+
end
1007+
@test Compat.reverse([1, 2, 3, 4]) == [4, 3, 2, 1]
1008+
@test Compat.reverse([1 2; 3 4], dims=1) == [3 4; 1 2]
1009+
@test Compat.reverse([1 2; 3 4], dims=2) == [2 1; 4 3]
1010+
1011+
# 0.7.0-DEV.4738
1012+
if VERSION < v"0.7.0-beta2.143"
1013+
@test squeeze([1 2], dims=1) == [1, 2]
1014+
@test_throws ArgumentError squeeze([1 2], dims=2)
1015+
@test_throws ArgumentError squeeze(hcat([1, 2]), dims=1)
1016+
@test squeeze(hcat([1, 2]), dims=2) == [1, 2]
1017+
@test_throws Exception squeeze([1,2])
1018+
end
1019+
1020+
# 0.7.0-DEV.5165
1021+
@test Compat.cat([1, 2], [3, 4, 5], dims = 1) == [1, 2, 3, 4, 5]
1022+
@test Compat.cat([1, 2], [3, 4], dims = 2) == [1 3; 2 4]
1023+
if VERSION < v"0.7.0-DEV.5165"
1024+
@test_throws UndefKeywordError Compat.cat([1, 2], [3, 4])
1025+
end

0 commit comments

Comments
 (0)