Skip to content
Open
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: 6 additions & 0 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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[]) == ""
Expand Down