Skip to content

Commit

Permalink
Simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Feb 5, 2024
1 parent 5bde556 commit fcabd33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
20 changes: 9 additions & 11 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -643,19 +643,17 @@ end

# In particular, these make iszero(Eye(n)) efficient.
# use any/all on scalar to get Boolean error message
any(f::Function, x::AbstractFill) = !isempty(x) && any(f(getindex_value(x)))
function any(f::Function, x::AbstractFill)
isempty(x) && return false
# If the condition is true for one value, then it's true for all
fval = f(getindex_value(x))
any((fval,))
end
function all(f::Function, x::AbstractFill)
isempty(x) && return true
# If the condition is true for one value, then it's true for all
fval = f(getindex_value(x))
# checking the Bool path before isempty(x) allows short-curcuiting
# in case the value is known from the type, e.g. in Zeros
if fval isa Bool
return fval || isempty(x)
elseif isempty(x) # cases like all(Fill(2,0))
return true
elseif ismissing(fval)
return missing
end
return all(fval)
return all((fval,))
end
any(x::AbstractFill) = any(identity, x)
all(x::AbstractFill) = all(identity, x)
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,10 @@ end
@test !any(Fill(2,0))
@test any(Trues(2,0)) == any(trues(2,0))
@test_throws TypeError all(Fill(2,2))
@test all(iszero, Fill(missing,2)) === all(iszero, fill(missing,2))
@test all(iszero, Fill(missing,0)) === all(iszero, fill(missing,0)) === true
@test all(iszero, Fill(missing,2)) === all(iszero, fill(missing,2)) === missing
@test any(iszero, Fill(missing,0)) === any(iszero, fill(missing,0)) === false
@test any(iszero, Fill(missing,2)) === any(iszero, fill(missing,2)) === missing
end

@testset "Error" begin
Expand Down

0 comments on commit fcabd33

Please sign in to comment.