Skip to content

Commit 8e5b07f

Browse files
stevengjmartinholters
authored andcommitted
commit for init arg in reduce/mapreduce
1 parent 7cd5de2 commit 8e5b07f

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ Currently, the `@compat` macro supports the following syntaxes:
286286
`Compat.median`, `Compat.minimum`, `Compat.prod`, `Compat.reduce`, `Compat.sort`,
287287
and `Compat.sum` with `dims` keyword argument ([#25989],[#26369]).
288288

289+
* `Compat.mapreduce` and `Compat.reduce` with `init` keyword argument ([#27711]).
290+
289291
* `selectdim` to obtain a view of an array with a specified index for a specified dimension ([#26009]).
290292

291293
* `squeeze` with `dims` as keyword argument ([#26660]).
@@ -658,3 +660,5 @@ includes this fix. Find the minimum version from there.
658660
[#27258]: https://github.com/JuliaLang/julia/issues/27258
659661
[#27298]: https://github.com/JuliaLang/julia/issues/27298
660662
[#27401]: https://github.com/JuliaLang/julia/issues/27401
663+
[#27711]: https://github.com/JuliaLang/julia/issues/27711
664+
[#27834]: https://github.com/JuliaLang/julia/issues/27834

src/Compat.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,16 +1780,19 @@ if VERSION < v"0.7.0-DEV.4064"
17801780
cor(a::AbstractMatrix; dims=nothing) = dims===nothing ? Base.cor(a) : Base.cor(a, dims)
17811781
cor(a::AbstractVecOrMat, b::AbstractVecOrMat; dims=nothing) =
17821782
dims===nothing ? Base.cor(a, b) : Base.cor(a, b, dims)
1783-
mapreduce(f, op, a::AbstractArray; dims=nothing) =
1784-
dims===nothing ? Base.mapreduce(f, op, a) : Base.mapreducedim(f, op, a, dims)
1785-
mapreduce(f, op, v0, a::AbstractArray; dims=nothing) =
1786-
dims===nothing ? Base.mapreduce(f, op, v0, a) : Base.mapreducedim(f, op, a, dims, v0)
1787-
reduce(op, a::AbstractArray; dims=nothing) =
1788-
dims===nothing ? Base.reduce(op, a) : Base.reducedim(op, a, dims)
1789-
reduce(op, v0, a::AbstractArray; dims=nothing) =
1790-
dims===nothing ? Base.reduce(op, v0, a) : Base.reducedim(op, a, dims, v0)
1783+
mapreduce(f, op, a::AbstractArray; dims=nothing, init=nothing) =
1784+
init === nothing ? (dims===nothing ? Base.mapreduce(f, op, a) : Base.mapreducedim(f, op, a, dims)) :
1785+
(dims===nothing ? Base.mapreduce(f, op, init, a) : Base.mapreducedim(f, op, a, dims, init))
1786+
reduce(op, a::AbstractArray; dims=nothing, init=nothing) =
1787+
init === nothing ? (dims===nothing ? Base.reduce(op, a) : Base.reducedim(op, a, dims)) :
1788+
(dims===nothing ? Base.reduce(op, init, a) : Base.reducedim(op, a, dims, init))
17911789
accumulate!(op, out, a; dims=nothing) =
17921790
dims===nothing ? Base.accumulate!(op, out, a) : Base.accumulate!(op, out, a, dims)
1791+
elseif VERSION < v"0.7.0-beta.81" # julia#27711
1792+
mapreduce(f, op, a::AbstractArray; dims=nothing, init=nothing) =
1793+
init === nothing ? Base.mapreduce(f, op, a; dims=dims) : Base.mapreduce(f, op, init, a; dims=dims)
1794+
reduce(op, a::AbstractArray; dims=nothing, init=nothing) =
1795+
init === nothing ? Base.reduce(op, a; dims=dims) : Base.reduce(op, init, a; dims=dims)
17931796
end
17941797
if VERSION < v"0.7.0-DEV.4534"
17951798
reverse(a::AbstractArray; dims=nothing) =

test/runtests.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,15 +1596,15 @@ end
15961596
@test Compat.mapreduce(string, *, [1 2; 3 4]) == "1324"
15971597
Issue26488 && @test Compat.mapreduce(string, *, [1 2; 3 4], dims=1) == ["13" "24"]
15981598
Issue26488 && @test Compat.mapreduce(string, *, [1 2; 3 4], dims=2) == hcat(["12", "34"])
1599-
@test Compat.mapreduce(string, *, "z", [1 2; 3 4]) == "z1324"
1600-
@test Compat.mapreduce(string, *, "z", [1 2; 3 4], dims=1) == ["z13" "z24"]
1601-
@test Compat.mapreduce(string, *, "z", [1 2; 3 4], dims=2) == hcat(["z12", "z34"])
1599+
@test Compat.mapreduce(string, *, [1 2; 3 4], init="z") == "z1324"
1600+
@test Compat.mapreduce(string, *, [1 2; 3 4], dims=1, init="z") == ["z13" "z24"]
1601+
@test Compat.mapreduce(string, *, [1 2; 3 4], dims=2, init="z") == hcat(["z12", "z34"])
16021602
@test Compat.reduce(*, [1 2; 3 4]) == 24
16031603
@test Compat.reduce(*, [1 2; 3 4], dims=1) == [3 8]
16041604
@test Compat.reduce(*, [1 2; 3 4], dims=2) == hcat([2, 12])
1605-
@test Compat.reduce(*, 10, [1 2; 3 4]) == 240
1606-
@test Compat.reduce(*, 10, [1 2; 3 4], dims=1) == [30 80]
1607-
@test Compat.reduce(*, 10, [1 2; 3 4], dims=2) == hcat([20, 120])
1605+
@test Compat.reduce(*, [1 2; 3 4], init=10) == 240
1606+
@test Compat.reduce(*, [1 2; 3 4], dims=1, init=10) == [30 80]
1607+
@test Compat.reduce(*, [1 2; 3 4], dims=2, init=10) == hcat([20, 120])
16081608
@test Compat.sort([1, 2, 3, 4]) == [1, 2, 3, 4]
16091609
@test Compat.sort([1 2; 3 4], dims=1) == [1 2; 3 4]
16101610
@test Compat.sort([1 2; 3 4], dims=2) == [1 2; 3 4]

0 commit comments

Comments
 (0)