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 29, 2019
1 parent 93c9ae4 commit 256814a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 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
10 changes: 4 additions & 6 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,12 @@ mapreduce_empty(f, op, T) = _empty_reduce_error()
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(::typeof(abs), ::typeof(max), T) = abs(zero(T))
mapreduce_empty(::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_iter(f, op, itr, ::EltypeUnknown) = mapreduce_empty(f, op, Any)


# handling of single-element iterators
"""
Expand Down
2 changes: 1 addition & 1 deletion test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ end
# issue #18695
test18695(r) = sum( t^2 for t in r )
@test @inferred(test18695([1.0,2.0,3.0,4.0])) == 30.0
@test_throws ArgumentError test18695(Any[])
@test_throws MethodError test18695(Any[])

# issue #21107
@test foldr(-,2:2) == 2
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 256814a

Please sign in to comment.