Skip to content

add 1-arg count, sum, prod, etc. methods; with tests #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ LRUCache = "1"
Mmap = "1"
OffsetArrays = "1"
Statistics = "1.9"
Suppressor = "0.2.8"
Test = "1.9"
TraceFuns = "0.2.0"
julia = "1.9"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
TraceFuns = "626f57eb-f6ca-464c-8f04-384fee2a352e"

[targets]
test = ["Aqua", "Statistics", "Test"]
test = ["Aqua", "Statistics", "Test", "Suppressor", "TraceFuns"]
4 changes: 3 additions & 1 deletion src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ end
# Implementation for special cases and if fallback breaks in future julia versions

for fname in [:sum, :prod, :all, :any, :minimum, :maximum]
@eval Base.$fname(v::AbstractDiskArray) = Base.$fname(identity, v::AbstractDiskArray)
@eval function Base.$fname(f::Function, v::AbstractDiskArray)
$fname(eachchunk(v)) do chunk
$fname(f, v[chunk...])
end
end
end

Base.count(v::AbstractDiskArray) = count(identity, v::AbstractDiskArray)
function Base.count(f, v::AbstractDiskArray)
sum(eachchunk(v)) do chunk
count(f, v[chunk...])
end
end
end
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using DiskArrays.TestTypes
using Test
using Statistics
using Aqua
using TraceFuns, Suppressor

# Run with any code changes
# using JET
Expand Down Expand Up @@ -1049,3 +1050,12 @@ end
@inferred Matrix{Int} map(identity, da)
@inferred Matrix{Float64} map(x -> x * 5.0, da)
end

@testset "identity function" begin
a = ChunkedDiskArray(1:10 .> 3; chunksize=(3, ))
for fname in [:sum, :prod, :all, :any, :minimum, :maximum, :count]
@eval out = @capture_out @trace $fname($a) DiskArrays
@test occursin("DiskGenerator", out) == false
end
@test count(a) + count(!, a) == length(a)
end
Loading