Skip to content

Commit c4743c9

Browse files
martinholtersstevengj
authored andcommitted
(map)reduce with init keyword (#590)
* commit for init arg in reduce/mapreduce * Keep (map)reduce with positional v0 but dims keyword argument on Julia 0.6 * Make Compat.(map)reduce work without given dims
1 parent a4c6f22 commit c4743c9

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-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]).
@@ -660,4 +662,6 @@ includes this fix. Find the minimum version from there.
660662
[#27258]: https://github.com/JuliaLang/julia/issues/27258
661663
[#27298]: https://github.com/JuliaLang/julia/issues/27298
662664
[#27401]: https://github.com/JuliaLang/julia/issues/27401
665+
[#27711]: https://github.com/JuliaLang/julia/issues/27711
663666
[#27828]: https://github.com/JuliaLang/julia/issues/27828
667+
[#27834]: https://github.com/JuliaLang/julia/issues/27834

src/Compat.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,16 +1780,26 @@ 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+
# kept for compatibility with early adopters
1792+
mapreduce(f, op, v0, a::AbstractArray; dims=nothing) =
1793+
mapreduce(f, op, a, dims=dims, init=v0)
1794+
reduce(op, v0, a::AbstractArray; dims=nothing) =
1795+
reduce(op, a, dims=dims, init=v0)
1796+
elseif VERSION < v"0.7.0-beta.81" # julia#27711
1797+
mapreduce(f, op, a::AbstractArray; dims=nothing, init=nothing) =
1798+
init === nothing ? (dims===nothing ? Base.mapreduce(f, op, a) : Base.mapreduce(f, op, a, dims=dims)) :
1799+
(dims===nothing ? Base.mapreduce(f, op, init, a) : Base.mapreduce(f, op, init, a, dims=dims))
1800+
reduce(op, a::AbstractArray; dims=nothing, init=nothing) =
1801+
init === nothing ? (dims===nothing ? Base.reduce(op, a) : Base.reduce(op, a, dims=dims)) :
1802+
(dims===nothing ? Base.reduce(op, init, a) : Base.reduce(op, init, a, dims=dims))
17931803
end
17941804
if VERSION < v"0.7.0-DEV.4534"
17951805
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)