Skip to content

Commit 0717843

Browse files
committed
Add mappedarrayreduce
1 parent 90c6fce commit 0717843

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/MappedArrays.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module MappedArrays
22

33
using Base: @propagate_inbounds
44

5-
export AbstractMappedArray, MappedArray, ReadonlyMappedArray, mappedarray, of_eltype
5+
export AbstractMappedArray, MappedArray, ReadonlyMappedArray, mappedarray, of_eltype, mappedarrayreduce
66

77
abstract type AbstractMappedArray{T,N} <: AbstractArray{T,N} end
88
abstract type AbstractMultiMappedArray{T,N} <: AbstractMappedArray{T,N} end
@@ -261,4 +261,9 @@ eltypes(A::AbstractArray) = Tuple{eltype(A)}
261261
## Deprecations
262262
@deprecate mappedarray(f_finv::Tuple{Any,Any}, args::AbstractArray...) mappedarray(f_finv[1], f_finv[2], args...)
263263

264+
265+
# mapreduce
266+
267+
mappedarrayreduce(f, op, A::AbstractArray...; kw...) = reduce(op, mappedarray(f, A...); kw...)
268+
264269
end # module

test/runtests.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,23 @@ end
177177
str = String(take!(io))
178178
@test occursin("x1 + x2", str)
179179
end
180+
181+
@testset "mapreduce" begin
182+
for T in [Int, Float64]
183+
x = rand(T, 10); y = similar(x);
184+
185+
f = x->x^2; op = +
186+
@test mapreduce(f, op, x) == mappedarrayreduce(f, op, x)
187+
@test mapreduce(f, op, x, init = zero(T)) == mappedarrayreduce(f, op, x, init = zero(T))
188+
@test mapreduce(f, op, x, init = zero(T), dims = 1) == mappedarrayreduce(f, op, x, init = zero(T), dims = 1)
189+
@test mapreduce(f, op, x, init = zero(T), dims = :) == mappedarrayreduce(f, op, x, init = zero(T), dims = :)
190+
191+
if VERSION >= v"1.2"
192+
f = ==; op = +
193+
@test mapreduce(f, op, x, y) == mappedarrayreduce(f, op, x, y)
194+
@test mapreduce(f, op, x, y, init = 0) == mappedarrayreduce(f, op, x, y, init = 0)
195+
@test mapreduce(f, op, x, y, init = 0, dims = 1) == mappedarrayreduce(f, op, x, y, init = 0, dims = 1)
196+
@test mapreduce(f, op, x, y, init = 0, dims = :) == mappedarrayreduce(f, op, x, y, init = 0, dims = :)
197+
end
198+
end
199+
end

0 commit comments

Comments
 (0)