Skip to content

Use one-arg op form for reduce_first (Alternative #58490) #58491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 3 additions & 12 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_sum(x, y) = x + y
add_sum(x::BitSignedSmall, y::BitSignedSmall) = Int(x) + Int(y)
add_sum(x::BitUnsignedSmall, y::BitUnsignedSmall) = UInt(x) + UInt(y)
add_sum(x::Real, y::Real)::Real = x + y
add_sum(x) = +(x)

"""
Base.mul_prod(x, y)
Expand All @@ -28,6 +29,7 @@ mul_prod(x, y) = x * y
mul_prod(x::BitSignedSmall, y::BitSignedSmall) = Int(x) * Int(y)
mul_prod(x::BitUnsignedSmall, y::BitUnsignedSmall) = UInt(x) * UInt(y)
mul_prod(x::Real, y::Real)::Real = x * y
mul_prod(x) = *(x)

and_all(x, y) = (x && y)::Bool
or_any(x, y) = (x || y)::Bool
Expand Down Expand Up @@ -397,18 +399,7 @@ The default is `x` for most types. The main purpose is to ensure type stability,
additional methods should only be defined for cases where `op` gives a result with
different types than its inputs.
"""
reduce_first(op, x) = x
reduce_first(::typeof(+), x::Bool) = Int(x)
reduce_first(::typeof(*), x::AbstractChar) = string(x)

reduce_first(::typeof(add_sum), x) = reduce_first(+, x)
reduce_first(::typeof(add_sum), x::BitSignedSmall) = Int(x)
reduce_first(::typeof(add_sum), x::BitUnsignedSmall) = UInt(x)
reduce_first(::typeof(mul_prod), x) = reduce_first(*, x)
reduce_first(::typeof(mul_prod), x::BitSignedSmall) = Int(x)
reduce_first(::typeof(mul_prod), x::BitUnsignedSmall) = UInt(x)
reduce_first(::typeof(vcat), x) = vcat(x)
reduce_first(::typeof(hcat), x) = hcat(x)
reduce_first(op, x) = applicable(op, x) ? op(x) : x

"""
Base.mapreduce_first(f, op, x)
Expand Down