Skip to content

Commit 48fd53d

Browse files
committed
Implement vararg methods for mapreduce similar to map, fixes #27704.
1 parent aee211b commit 48fd53d

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

NEWS.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Standard library changes
5656
* `nextfloat(::BigFloat)` and `prevfloat(::BigFloat)` now returns a value with the same precision
5757
as their argument, which means that (in particular) `nextfloat(prevfloat(x)) == x` whereas
5858
previously this could result in a completely different value with a different precision ([#31310])
59+
* `mapreduce` now accept multiple iterators, similar to `map` ([#31532]).
5960

6061
#### LinearAlgebra
6162

@@ -103,6 +104,7 @@ Deprecated or removed
103104

104105
<!--- generated by NEWS-update.jl: -->
105106
[#21598]: https://github.com/JuliaLang/julia/issues/21598
107+
[#22922]: https://github.com/JuliaLang/julia/issues/22922
106108
[#24980]: https://github.com/JuliaLang/julia/issues/24980
107109
[#28850]: https://github.com/JuliaLang/julia/issues/28850
108110
[#29777]: https://github.com/JuliaLang/julia/issues/29777
@@ -112,16 +114,27 @@ Deprecated or removed
112114
[#30200]: https://github.com/JuliaLang/julia/issues/30200
113115
[#30298]: https://github.com/JuliaLang/julia/issues/30298
114116
[#30323]: https://github.com/JuliaLang/julia/issues/30323
115-
[#30349]: https://github.com/JuliaLang/julia/issues/30349
116117
[#30372]: https://github.com/JuliaLang/julia/issues/30372
117118
[#30382]: https://github.com/JuliaLang/julia/issues/30382
118119
[#30577]: https://github.com/JuliaLang/julia/issues/30577
119120
[#30583]: https://github.com/JuliaLang/julia/issues/30583
120121
[#30584]: https://github.com/JuliaLang/julia/issues/30584
121122
[#30593]: https://github.com/JuliaLang/julia/issues/30593
123+
[#30604]: https://github.com/JuliaLang/julia/issues/30604
122124
[#30618]: https://github.com/JuliaLang/julia/issues/30618
123125
[#30670]: https://github.com/JuliaLang/julia/issues/30670
124126
[#30712]: https://github.com/JuliaLang/julia/issues/30712
125127
[#30724]: https://github.com/JuliaLang/julia/issues/30724
126128
[#30915]: https://github.com/JuliaLang/julia/issues/30915
127129
[#30919]: https://github.com/JuliaLang/julia/issues/30919
130+
[#30938]: https://github.com/JuliaLang/julia/issues/30938
131+
[#31008]: https://github.com/JuliaLang/julia/issues/31008
132+
[#31009]: https://github.com/JuliaLang/julia/issues/31009
133+
[#31125]: https://github.com/JuliaLang/julia/issues/31125
134+
[#31211]: https://github.com/JuliaLang/julia/issues/31211
135+
[#31230]: https://github.com/JuliaLang/julia/issues/31230
136+
[#31235]: https://github.com/JuliaLang/julia/issues/31235
137+
[#31310]: https://github.com/JuliaLang/julia/issues/31310
138+
[#31441]: https://github.com/JuliaLang/julia/issues/31441
139+
[#31451]: https://github.com/JuliaLang/julia/issues/31451
140+
[#31532]: https://github.com/JuliaLang/julia/issues/31532

base/reduce.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ mapreduce_impl(f, op, A::AbstractArray, ifirst::Integer, ilast::Integer) =
179179
mapreduce_impl(f, op, A, ifirst, ilast, pairwise_blocksize(f, op))
180180

181181
"""
182-
mapreduce(f, op, itr; [init])
182+
mapreduce(f, op, itrs...; [init])
183183
184-
Apply function `f` to each element in `itr`, and then reduce the result using the binary
184+
Apply function `f` to each element(s) in `itrs`, and then reduce the result using the binary
185185
function `op`. If provided, `init` must be a neutral element for `op` that will be returned
186186
for empty collections. It is unspecified whether `init` is used for non-empty collections.
187187
In general, it will be necessary to provide `init` to work with empty collections.
@@ -191,6 +191,9 @@ In general, it will be necessary to provide `init` to work with empty collection
191191
intermediate collection needs to be created. See documentation for [`reduce`](@ref) and
192192
[`map`](@ref).
193193
194+
!!! compat "Julia 1.2"
195+
`mapreduce` with multiple iterators requires Julia 1.2 or later.
196+
194197
# Examples
195198
```jldoctest
196199
julia> mapreduce(x->x^2, +, [1:3;]) # == 1 + 4 + 9
@@ -203,6 +206,7 @@ implementations may reuse the return value of `f` for elements that appear multi
203206
guaranteed left or right associativity and invocation of `f` for every value.
204207
"""
205208
mapreduce(f, op, itr; kw...) = mapfoldl(f, op, itr; kw...)
209+
mapreduce(f, op, itrs...; kw...) = reduce(op, Generator(f, itrs...); kw...)
206210

207211
# Note: sum_seq usually uses four or more accumulators after partial
208212
# unrolling, so each accumulator gets at most 256 numbers

base/reducedim.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,14 @@ reducedim!(op, R::AbstractArray{RT}, A::AbstractArray) where {RT} =
278278
mapreducedim!(identity, op, R, A)
279279

280280
"""
281-
mapreduce(f, op, A::AbstractArray; dims=:, [init])
281+
mapreduce(f, op, A::AbstractArray...; dims=:, [init])
282282
283283
Evaluates to the same as `reduce(op, map(f, A); dims=dims, init=init)`, but is generally
284284
faster because the intermediate array is avoided.
285285
286+
!!! compat "Julia 1.2"
287+
`mapreduce` with multiple iterators requires Julia 1.2 or later.
288+
286289
# Examples
287290
```jldoctest
288291
julia> a = reshape(Vector(1:16), (4,4))
@@ -302,6 +305,7 @@ julia> mapreduce(isodd, |, a, dims=1)
302305
```
303306
"""
304307
mapreduce(f, op, A::AbstractArray; dims=:, kw...) = _mapreduce_dim(f, op, kw.data, A, dims)
308+
mapreduce(f, op, A::AbstractArray...; kw...) = reduce(op, map(f, A...); kw...)
305309

306310
_mapreduce_dim(f, op, nt::NamedTuple{(:init,)}, A::AbstractArray, ::Colon) = mapfoldl(f, op, A; nt...)
307311

test/reduce.jl

+21
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ using .Main.OffsetArrays
4343
@test mapreduce(-, +, [-10 -9 -3]) == ((10 + 9) + 3)
4444
@test mapreduce((x)->x[1:3], (x,y)->"($x+$y)", ["abcd", "efgh", "01234"]) == "((abc+efg)+012)"
4545

46+
# mapreduce with multiple iterators
47+
@test mapreduce(*, +, (i for i in 2:3), (i for i in 4:5)) == 23
48+
@test mapreduce(*, +, (i for i in 2:3), (i for i in 4:5); init = 2) == 25
49+
@test mapreduce(*, (x,y)->"($x+$y)", ["a", "b", "c"], ["d", "e", "f"]) == "((ad+be)+cf)"
50+
@test mapreduce(*, (x,y)->"($x+$y)", ["a", "b", "c"], ["d", "e", "f"]; init = "gh") ==
51+
"(((gh+ad)+be)+cf)"
52+
53+
@test mapreduce(*, +, [2, 3], [4, 5]) == 23
54+
@test mapreduce(*, +, [2, 3], [4, 5]; init = 2) == 25
55+
@test mapreduce(*, +, [2, 3], [4, 5]; dims = 1) == [23]
56+
@test mapreduce(*, +, [2, 3], [4, 5]; dims = 1, init = 2) == [25]
57+
@test mapreduce(*, +, [2, 3], [4, 5]; dims = 2) == [8, 15]
58+
@test mapreduce(*, +, [2, 3], [4, 5]; dims = 2, init = 2) == [10, 17]
59+
60+
@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]) == 110
61+
@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; init = 2) == 112
62+
@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 1) == [44 66]
63+
@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 1, init = 2) == [46 68]
64+
@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 2) == reshape([33, 77], :, 1)
65+
@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 2, init = 2) == reshape([35, 79], :, 1)
66+
4667
# mapreduce() for 1- 2- and n-sized blocks (PR #19325)
4768
@test mapreduce(-, +, [-10]) == 10
4869
@test mapreduce(abs2, +, [-9, -3]) == 81 + 9

0 commit comments

Comments
 (0)