Skip to content

Commit 09617ac

Browse files
authored
add more defaults for reductions over empty arrays (#29919)
From discussion in #28535
1 parent dabb93a commit 09617ac

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

base/reduce.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ reduce_empty(::typeof(*), ::Type{<:AbstractChar}) = ""
348348
reduce_empty(::typeof(&), ::Type{Bool}) = true
349349
reduce_empty(::typeof(|), ::Type{Bool}) = false
350350

351+
reduce_empty(::typeof(max), T) = typemin(T)
352+
reduce_empty(::typeof(min), T) = typemax(T)
353+
351354
reduce_empty(::typeof(add_sum), ::Type{Union{}}) = _empty_reduce_error(add_sum, Union{})
352355
reduce_empty(::typeof(add_sum), ::Type{T}) where {T} = reduce_empty(+, T)
353356
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:SmallSigned} = zero(Int)
@@ -373,8 +376,10 @@ mapreduce_empty(::typeof(identity), op, T) = reduce_empty(op, T)
373376
mapreduce_empty(::typeof(abs), op, T) = abs(reduce_empty(op, T))
374377
mapreduce_empty(::typeof(abs2), op, T) = abs2(reduce_empty(op, T))
375378

376-
mapreduce_empty(f::typeof(abs), ::typeof(max), T) = abs(zero(T))
377-
mapreduce_empty(f::typeof(abs2), ::typeof(max), T) = abs2(zero(T))
379+
mapreduce_empty(::typeof(abs), ::typeof(max), T) = abs(zero(T))
380+
mapreduce_empty(::typeof(abs), ::typeof(min), T) = typemax(abs(zero(T)))
381+
mapreduce_empty(::typeof(abs2), ::typeof(max), T) = abs2(zero(T))
382+
mapreduce_empty(::typeof(abs2), ::typeof(min), T) = typemax(abs2(zero(T)))
378383

379384
# For backward compatibility:
380385
mapreduce_empty_iter(f, op, itr, ItrEltype) =

test/reduce.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ end
9191
@test mapreduce(abs2, *, Float64[]) === 1.0
9292
@test mapreduce(abs2, max, Float64[]) === 0.0
9393
@test mapreduce(abs, max, Float64[]) === 0.0
94+
@test mapreduce(abs2, min, Float64[]) === Inf
95+
@test mapreduce(abs, min, Float64[]) === Inf
96+
@test_throws ArgumentError mapreduce(abs2, &, Float64[])
97+
@test_throws ArgumentError mapreduce(abs2, |, Float64[])
9498
@test_throws ["reducing over an empty collection is not allowed",
9599
"consider supplying `init`"] mapreduce(abs2, &, Float64[])
96100
@test_throws str -> !occursin("Closest candidates are", str) mapreduce(abs2, &, Float64[])
@@ -260,6 +264,21 @@ prod2(itr) = invoke(prod, Tuple{Any}, itr)
260264
@test minimum(sin, []; init=1) == 1
261265
@test extrema(sin, []; init=(1, -1)) == (1, -1)
262266

267+
@test maximum(Float64[]) === -Inf
268+
@test minimum(Float64[]) === +Inf
269+
270+
@test maximum(Float32[]) === -Inf32
271+
@test minimum(Float32[]) === +Inf32
272+
273+
@test maximum(abs, Int[]) === 0
274+
@test_throws ArgumentError minimum(abs, Int[])
275+
276+
@test maximum(abs, Float64[]) === 0.0
277+
@test minimum(abs, Float64[]) === +Inf
278+
279+
@test maximum(abs, Float32[]) === 0.0f0
280+
@test minimum(abs, Float32[]) === +Inf32
281+
263282
@test maximum(5) == 5
264283
@test minimum(5) == 5
265284
@test extrema(5) == (5, 5)

0 commit comments

Comments
 (0)