Skip to content

Commit

Permalink
Add mappedarrayreduce
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Nov 24, 2020
1 parent 90c6fce commit 0717843
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/MappedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module MappedArrays

using Base: @propagate_inbounds

export AbstractMappedArray, MappedArray, ReadonlyMappedArray, mappedarray, of_eltype
export AbstractMappedArray, MappedArray, ReadonlyMappedArray, mappedarray, of_eltype, mappedarrayreduce

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


# mapreduce

mappedarrayreduce(f, op, A::AbstractArray...; kw...) = reduce(op, mappedarray(f, A...); kw...)

end # module
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,23 @@ end
str = String(take!(io))
@test occursin("x1 + x2", str)
end

@testset "mapreduce" begin
for T in [Int, Float64]
x = rand(T, 10); y = similar(x);

f = x->x^2; op = +
@test mapreduce(f, op, x) == mappedarrayreduce(f, op, x)
@test mapreduce(f, op, x, init = zero(T)) == mappedarrayreduce(f, op, x, init = zero(T))
@test mapreduce(f, op, x, init = zero(T), dims = 1) == mappedarrayreduce(f, op, x, init = zero(T), dims = 1)
@test mapreduce(f, op, x, init = zero(T), dims = :) == mappedarrayreduce(f, op, x, init = zero(T), dims = :)

if VERSION >= v"1.2"
f = ==; op = +
@test mapreduce(f, op, x, y) == mappedarrayreduce(f, op, x, y)
@test mapreduce(f, op, x, y, init = 0) == mappedarrayreduce(f, op, x, y, init = 0)
@test mapreduce(f, op, x, y, init = 0, dims = 1) == mappedarrayreduce(f, op, x, y, init = 0, dims = 1)
@test mapreduce(f, op, x, y, init = 0, dims = :) == mappedarrayreduce(f, op, x, y, init = 0, dims = :)
end
end
end

0 comments on commit 0717843

Please sign in to comment.