Skip to content

Commit

Permalink
Remove definitions for reduce over &/| over empty collections o…
Browse files Browse the repository at this point in the history
…f unknown eltype.

Fixes #31635/#31837.
  • Loading branch information
simonbyrne committed Apr 26, 2019
1 parent 93c9ae4 commit 01d9b81
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ New language features
Language changes
----------------

* `reduce`/`mapreduce` using `&` and `|` as an operator over empty collections of unknown element type will throw an error instead of returning `true`/`false` ([#31635], [#31837]).

Multi-threading changes
-----------------------
Expand Down
17 changes: 6 additions & 11 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ end
function mapfoldl_impl(f, op, nt::NamedTuple{()}, itr)
y = iterate(itr)
if y === nothing
return Base.mapreduce_empty_iter(f, op, itr, IteratorEltype(itr))
return Base.mapreduce_empty(f, op, eltype(itr))
end
(x, i) = y
init = mapreduce_first(f, op, x)
Expand Down Expand Up @@ -110,7 +110,7 @@ end

function mapfoldr_impl(f, op, ::NamedTuple{()}, itr, i::Integer)
if isempty(itr)
return Base.mapreduce_empty_iter(f, op, itr, IteratorEltype(itr))
return Base.mapreduce_empty(f, op, eltype(itr))
end
return mapfoldr_impl(f, op, (init=mapreduce_first(f, op, itr[i]),), itr, i-1)
end
Expand Down Expand Up @@ -228,9 +228,9 @@ with reduction `op` over an empty array with element type of `T`.
If not defined, this will throw an `ArgumentError`.
"""
reduce_empty(op, T) = _empty_reduce_error()
reduce_empty(::typeof(+), T) = zero(T)
reduce_empty(::typeof(+), ::Type{<:Number}) = zero(T)
reduce_empty(::typeof(+), ::Type{Bool}) = zero(Int)
reduce_empty(::typeof(*), T) = one(T)
reduce_empty(::typeof(*), ::Type{<:Number}) = one(T)
reduce_empty(::typeof(*), ::Type{<:AbstractChar}) = ""
reduce_empty(::typeof(&), ::Type{Bool}) = true
reduce_empty(::typeof(|), ::Type{Bool}) = false
Expand All @@ -256,13 +256,8 @@ mapreduce_empty(::typeof(identity), op, T) = reduce_empty(op, T)
mapreduce_empty(::typeof(abs), op, T) = abs(reduce_empty(op, T))
mapreduce_empty(::typeof(abs2), op, T) = abs2(reduce_empty(op, T))

mapreduce_empty(f::typeof(abs), ::typeof(max), T) = abs(zero(T))
mapreduce_empty(f::typeof(abs2), ::typeof(max), T) = abs2(zero(T))

mapreduce_empty_iter(f, op, itr, ::HasEltype) = mapreduce_empty(f, op, eltype(itr))
mapreduce_empty_iter(f, op::typeof(&), itr, ::EltypeUnknown) = true
mapreduce_empty_iter(f, op::typeof(|), itr, ::EltypeUnknown) = false
mapreduce_empty_iter(f, op, itr, ::EltypeUnknown) = _empty_reduce_error()
mapreduce_empty(::typeof(abs), ::typeof(max), T) = abs(zero(T))
mapreduce_empty(::typeof(abs2), ::typeof(max), T) = abs2(zero(T))

# handling of single-element iterators
"""
Expand Down
2 changes: 1 addition & 1 deletion test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ call2(f,x,y) = f(x,y)
@test (call2(42,1) do x,y; x+y+1 end) == 44

# definitions using comparison syntax
let ab = reduce(&, x b for x in a) && length(b)>length(a)
let ab = all(x b for x in a) && length(b)>length(a)
@test [1,2] [1,2,3,4]
@test !([1,2] [1,3,4])
@test !([1,2] [1,2])
Expand Down

0 comments on commit 01d9b81

Please sign in to comment.