Skip to content

Commit

Permalink
fixes based on @TotalVerb's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Dec 13, 2017
1 parent 69114af commit eae9a54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ reduce_empty(op, T) = _empty_reduce_error()
reduce_empty(::typeof(+), T) = zero(T)
reduce_empty(::typeof(+), ::Type{Bool}) = zero(Int)
reduce_empty(::typeof(*), T) = one(T)
reduce_empty(::typeof(*), ::Type{Char}) = ""
reduce_empty(::typeof(&), ::Type{Bool}) = true
reduce_empty(::typeof(|), ::Type{Bool}) = false

Expand Down Expand Up @@ -303,6 +304,7 @@ The default is `x`.
"""
reduce_single(op, x) = x
reduce_single(::typeof(+), x::Bool) = Int(x)
reduce_single(::typeof(*), x::Char) = string(x)

reduce_single(::typeof(add_sum), x) = reduce_single(+, x)
reduce_single(::typeof(add_sum), x::SmallSigned) = Int(x)
Expand Down Expand Up @@ -489,7 +491,7 @@ function mapreduce_impl(f, op::Union{typeof(scalarmax),
A::AbstractArray, first::Int, last::Int)
# locate the first non NaN number
@inbounds a1 = A[first]
v = f(a1)
v = mapreduce_single(f, op, a1)
i = first + 1
while (v == v) && (i <= last)
@inbounds ai = A[i]
Expand Down
5 changes: 5 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,8 @@ test18695(r) = sum( t^2 for t in r )
# test neutral element not picked incorrectly for &, |
@test @inferred(foldl(&, Int[1])) === 1
@test_throws ArgumentError foldl(&, Int[])

# prod on Chars
@test prod(Char[]) == ""
@test prod(Char['a']) == "a"
@test prod(Char['a','b']) == "ab"

0 comments on commit eae9a54

Please sign in to comment.