Skip to content

Commit

Permalink
commit for init arg in reduce/mapreduce
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Jun 29, 2018
1 parent 05f941f commit 73abfa1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ Currently, the `@compat` macro supports the following syntaxes:
`Compat.median`, `Compat.minimum`, `Compat.prod`, `Compat.reduce`, `Compat.sort`,
and `Compat.sum` with `dims` keyword argument ([#25989],[#26369]).

* `Compat.mapreduce` and `Compat.reduce` with `init` keyword argument ([#27711]).

* `selectdim` to obtain a view of an array with a specified index for a specified dimension ([#26009]).

* `squeeze` with `dims` as keyword argument ([#26660]).
Expand Down Expand Up @@ -656,3 +658,5 @@ includes this fix. Find the minimum version from there.
[#27258]: https://github.com/JuliaLang/julia/issues/27258
[#27298]: https://github.com/JuliaLang/julia/issues/27298
[#27401]: https://github.com/JuliaLang/julia/issues/27401
[#27711]: https://github.com/JuliaLang/julia/issues/27711
[#27834]: https://github.com/JuliaLang/julia/issues/27834
19 changes: 11 additions & 8 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1781,16 +1781,19 @@ if VERSION < v"0.7.0-DEV.4064"
end
end
if VERSION < v"0.7.0-DEV.4064"
mapreduce(f, op, a::AbstractArray; dims=nothing) =
dims===nothing ? Base.mapreduce(f, op, a) : Base.mapreducedim(f, op, a, dims)
mapreduce(f, op, v0, a::AbstractArray; dims=nothing) =
dims===nothing ? Base.mapreduce(f, op, v0, a) : Base.mapreducedim(f, op, a, dims, v0)
reduce(op, a::AbstractArray; dims=nothing) =
dims===nothing ? Base.reduce(op, a) : Base.reducedim(op, a, dims)
reduce(op, v0, a::AbstractArray; dims=nothing) =
dims===nothing ? Base.reduce(op, v0, a) : Base.reducedim(op, a, dims, v0)
mapreduce(f, op, a::AbstractArray; dims=nothing, init=nothing) =
init === nothing ? (dims===nothing ? Base.mapreduce(f, op, a) : Base.mapreducedim(f, op, a, dims)) :
(dims===nothing ? Base.mapreduce(f, op, init, a) : Base.mapreducedim(f, op, a, dims, init))
reduce(op, a::AbstractArray; dims=nothing, init=nothing) =
init === nothing ? (dims===nothing ? Base.reduce(op, a) : Base.reducedim(op, a, dims)) :
(dims===nothing ? Base.reduce(op, init, a) : Base.reducedim(op, a, dims, init))
accumulate!(op, out, a; dims=nothing) =
dims===nothing ? Base.accumulate!(op, out, a) : Base.accumulate!(op, out, a, dims)
elseif VERSION < v"0.7.0-beta.81" # julia#27711
mapreduce(f, op, a::AbstractArray; dims=nothing, init=nothing) =
init === nothing ? Base.mapreduce(f, op, a; dims=dims) : Base.mapreduce(f, op, init, a; dims=dims)
reduce(op, a::AbstractArray; dims=nothing, init=nothing) =
init === nothing ? Base.reduce(op, a; dims=dims) : Base.reduce(op, init, a; dims=dims)
end
if VERSION < v"0.7.0-DEV.4534"
reverse(a::AbstractArray; dims=nothing) =
Expand Down
12 changes: 6 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1560,15 +1560,15 @@ end
@test Compat.mapreduce(string, *, [1 2; 3 4]) == "1324"
Issue26488 && @test Compat.mapreduce(string, *, [1 2; 3 4], dims=1) == ["13" "24"]
Issue26488 && @test Compat.mapreduce(string, *, [1 2; 3 4], dims=2) == hcat(["12", "34"])
@test Compat.mapreduce(string, *, "z", [1 2; 3 4]) == "z1324"
@test Compat.mapreduce(string, *, "z", [1 2; 3 4], dims=1) == ["z13" "z24"]
@test Compat.mapreduce(string, *, "z", [1 2; 3 4], dims=2) == hcat(["z12", "z34"])
@test Compat.mapreduce(string, *, [1 2; 3 4], init="z") == "z1324"
@test Compat.mapreduce(string, *, [1 2; 3 4], dims=1, init="z") == ["z13" "z24"]
@test Compat.mapreduce(string, *, [1 2; 3 4], dims=2, init="z") == hcat(["z12", "z34"])
@test Compat.reduce(*, [1 2; 3 4]) == 24
@test Compat.reduce(*, [1 2; 3 4], dims=1) == [3 8]
@test Compat.reduce(*, [1 2; 3 4], dims=2) == hcat([2, 12])
@test Compat.reduce(*, 10, [1 2; 3 4]) == 240
@test Compat.reduce(*, 10, [1 2; 3 4], dims=1) == [30 80]
@test Compat.reduce(*, 10, [1 2; 3 4], dims=2) == hcat([20, 120])
@test Compat.reduce(*, [1 2; 3 4], init=10) == 240
@test Compat.reduce(*, [1 2; 3 4], dims=1, init=10) == [30 80]
@test Compat.reduce(*, [1 2; 3 4], dims=2, init=10) == hcat([20, 120])
@test Compat.sort([1, 2, 3, 4]) == [1, 2, 3, 4]
@test Compat.sort([1 2; 3 4], dims=1) == [1 2; 3 4]
@test Compat.sort([1 2; 3 4], dims=2) == [1 2; 3 4]
Expand Down

0 comments on commit 73abfa1

Please sign in to comment.