diff --git a/NEWS.md b/NEWS.md index 012e6747ec3a5..8839c16523592 100644 --- a/NEWS.md +++ b/NEWS.md @@ -49,6 +49,7 @@ Standard library changes * A no-argument construct to `Ptr{T}` has been added which constructs a null pointer ([#30919]) * `strip` now accepts a function argument in the same manner as `lstrip` and `rstrip` ([#31211]) * `mktempdir` now accepts a `prefix` keyword argument to customize the file name ([#31230], [#22922]) +* `mapreduce` now accept multiple iterators, similar to `map` ([#?????]). #### LinearAlgebra diff --git a/base/reduce.jl b/base/reduce.jl index 4149b4c69e4e0..0eeb6ced0be54 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -179,9 +179,9 @@ mapreduce_impl(f, op, A::AbstractArray, ifirst::Integer, ilast::Integer) = mapreduce_impl(f, op, A, ifirst, ilast, pairwise_blocksize(f, op)) """ - mapreduce(f, op, itr; [init]) + mapreduce(f, op, itrs...; [init]) -Apply function `f` to each element in `itr`, and then reduce the result using the binary +Apply function `f` to each element(s) in `itrs`, and then reduce the result using the binary function `op`. If provided, `init` must be a neutral element for `op` that will be returned for empty collections. It is unspecified whether `init` is used for non-empty collections. 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 intermediate collection needs to be created. See documentation for [`reduce`](@ref) and [`map`](@ref). +!!! compat "Julia 1.2" + `mapreduce` with multiple iterators requires Julia 1.2 or later. + # Examples ```jldoctest 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 guaranteed left or right associativity and invocation of `f` for every value. """ mapreduce(f, op, itr; kw...) = mapfoldl(f, op, itr; kw...) +mapreduce(f, op, itrs...; kw...) = reduce(op, Generator(f, itrs...); kw...) # Note: sum_seq usually uses four or more accumulators after partial # unrolling, so each accumulator gets at most 256 numbers diff --git a/base/reducedim.jl b/base/reducedim.jl index 7b005aeff4f10..44a72a9a25b9d 100644 --- a/base/reducedim.jl +++ b/base/reducedim.jl @@ -278,11 +278,14 @@ reducedim!(op, R::AbstractArray{RT}, A::AbstractArray) where {RT} = mapreducedim!(identity, op, R, A) """ - mapreduce(f, op, A::AbstractArray; dims=:, [init]) + mapreduce(f, op, A::AbstractArray...; dims=:, [init]) Evaluates to the same as `reduce(op, map(f, A); dims=dims, init=init)`, but is generally faster because the intermediate array is avoided. +!!! compat "Julia 1.2" + `mapreduce` with multiple iterators requires Julia 1.2 or later. + # Examples ```jldoctest julia> a = reshape(Vector(1:16), (4,4)) @@ -302,6 +305,7 @@ julia> mapreduce(isodd, |, a, dims=1) ``` """ mapreduce(f, op, A::AbstractArray; dims=:, kw...) = _mapreduce_dim(f, op, kw.data, A, dims) +mapreduce(f, op, A::AbstractArray...; kw...) = reduce(op, map(f, A...); kw...) _mapreduce_dim(f, op, nt::NamedTuple{(:init,)}, A::AbstractArray, ::Colon) = mapfoldl(f, op, A; nt...) diff --git a/test/reduce.jl b/test/reduce.jl index 190bbe60aedfa..cc4ce66d2d7d4 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -43,6 +43,27 @@ using .Main.OffsetArrays @test mapreduce(-, +, [-10 -9 -3]) == ((10 + 9) + 3) @test mapreduce((x)->x[1:3], (x,y)->"($x+$y)", ["abcd", "efgh", "01234"]) == "((abc+efg)+012)" +# mapreduce with multiple iterators +@test mapreduce(*, +, (i for i in 2:3), (i for i in 4:5)) == 23 +@test mapreduce(*, +, (i for i in 2:3), (i for i in 4:5); init = 2) == 25 +@test mapreduce(*, (x,y)->"($x+$y)", ["a", "b", "c"], ["d", "e", "f"]) == "((ad+be)+cf)" +@test mapreduce(*, (x,y)->"($x+$y)", ["a", "b", "c"], ["d", "e", "f"]; init = "gh") == + "(((gh+ad)+be)+cf)" + +@test mapreduce(*, +, [2, 3], [4, 5]) == 23 +@test mapreduce(*, +, [2, 3], [4, 5]; init = 2) == 25 +@test mapreduce(*, +, [2, 3], [4, 5]; dims = 1) == [23] +@test mapreduce(*, +, [2, 3], [4, 5]; dims = 1, init = 2) == [25] +@test mapreduce(*, +, [2, 3], [4, 5]; dims = 2) == [8, 15] +@test mapreduce(*, +, [2, 3], [4, 5]; dims = 2, init = 2) == [10, 17] + +@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]) == 110 +@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; init = 2) == 112 +@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 1) == [44 66] +@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 1, init = 2) == [46 68] +@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 2) == reshape([33, 77], :, 1) +@test mapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 2, init = 2) == reshape([35, 79], :, 1) + # mapreduce() for 1- 2- and n-sized blocks (PR #19325) @test mapreduce(-, +, [-10]) == 10 @test mapreduce(abs2, +, [-9, -3]) == 81 + 9