diff --git a/base/reduce.jl b/base/reduce.jl index 2cd2f9518ee03..9392a2776a4a9 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -359,6 +359,12 @@ reduce_empty(::typeof(mul_prod), ::Type{Union{}}) = _empty_reduce_error(mul_prod reduce_empty(::typeof(mul_prod), ::Type{T}) where {T} = reduce_empty(*, T) reduce_empty(::typeof(mul_prod), ::Type{T}) where {T<:SmallSigned} = one(Int) reduce_empty(::typeof(mul_prod), ::Type{T}) where {T<:SmallUnsigned} = one(UInt) +reduce_empty(::typeof(&), ::Type{Bool}) = true +reduce_empty(::typeof(&), ::Type{T}) where {T<:Integer} = -1 % T +reduce_empty(::typeof(|), ::Type{Bool}) = false +reduce_empty(::typeof(|), ::Type{T}) where {T<:Integer} = zero(T) +reduce_empty(::typeof(xor), ::Type{Bool}) = false +reduce_empty(::typeof(xor), ::Type{T}) where {T<:Integer} = zero(T) reduce_empty(op::BottomRF, ::Type{T}) where {T} = reduce_empty(op.rf, T) reduce_empty(op::MappingRF, ::Type{T}) where {T} = mapreduce_empty(op.f, op.rf, T) diff --git a/test/reduce.jl b/test/reduce.jl index eb4004a1baf42..da0e10dcc37f6 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -11,7 +11,14 @@ using .Main.OffsetArrays @test foldl(+, Int64[]) === Int64(0) # In reference to issues #7465/#20144 (PR #20160) @test foldl(+, Int16[]) === Int16(0) # In reference to issues #21536 @test foldl(-, 1:5) == -13 +@test foldl(-, 10, 1:5) == -5 @test foldl(-, 1:5; init=10) == -5 +@test foldl(&, UInt8[]) === typemax(UInt8) +@test foldl(&, Int8[]) === Int8(-1) +@test foldl(|, UInt8[]) === zero(UInt8) +@test foldl(|, Int8[]) === zero(Int8) +@test foldl(⊻, UInt8[]) === zero(UInt8) +@test foldl(⊻, Int8[]) === zero(Int8) @test Base.mapfoldl(abs2, -, 2:5) == -46 @test Base.mapfoldl(abs2, -, 2:5; init=10) == -44 @@ -645,8 +652,7 @@ test18695(r) = sum( t^2 for t in r ) # test neutral element not picked incorrectly for &, | @test @inferred(foldl(&, Int[1])) === 1 -@test_throws ["reducing over an empty", - "consider supplying `init`"] foldl(&, Int[]) +@test foldl(&, Int[]) === -1 # prod on Chars @test prod(Char[]) == ""