Skip to content
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

workaround for #35800, inference issue in mapreduce #37105

Merged
merged 1 commit into from
Aug 18, 2020
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
workaround for #35800, inference issue in mapreduce
  • Loading branch information
JeffBezanson committed Aug 18, 2020
commit df7343450b6e83c11312f3d548d7da8c15262d70
10 changes: 9 additions & 1 deletion base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,15 @@ julia> count([true, false, true, true])
"""
count(itr) = count(identity, itr)

count(f, itr) = mapreduce(_bool(f), add_sum, itr, init=0)
count(f, itr) = _simple_count(f, itr)

function _simple_count(pred, itr)
n = 0
for x in itr
n += pred(x)::Bool
end
return n
end

function count(::typeof(identity), x::Array{Bool})
n = 0
Expand Down
5 changes: 4 additions & 1 deletion base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,10 @@ julia> count(<=(2), A, dims=2)
```
"""
count(A::AbstractArrayOrBroadcasted; dims=:) = count(identity, A, dims=dims)
count(f, A::AbstractArrayOrBroadcasted; dims=:) = mapreduce(_bool(f), add_sum, A, dims=dims, init=0)
count(f, A::AbstractArrayOrBroadcasted; dims=:) = _count(f, A, dims)

_count(f, A::AbstractArrayOrBroadcasted, dims::Colon) = _simple_count(f, A)
_count(f, A::AbstractArrayOrBroadcasted, dims) = mapreduce(_bool(f), add_sum, A, dims=dims, init=0)

"""
count!([f=identity,] r, A)
Expand Down
4 changes: 4 additions & 0 deletions test/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ using Random

# main tests

# issue #35800
# tested very early since it can be state-dependent
@test @inferred(mapreduce(x->count(!iszero,x), +, [rand(1)]; init = 0.)) == 1.0

function safe_mapslices(op, A, region)
newregion = intersect(region, 1:ndims(A))
return isempty(newregion) ? A : mapslices(op, A, dims = newregion)
Expand Down