Skip to content
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

Specialize common reductions for ranges with init specified #56896

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1155,3 +1155,10 @@ function _simple_count(::typeof(identity), x::Array{Bool}, init::T=0) where {T}
end
return n
end

# A few common reductions for ranges with init specified
for (fred, f) in ((maximum, max), (minimum, min), (sum, add_sum))
@eval function _foldl_impl(op::typeof(BottomRF($f)), init, r::AbstractRange)
isempty(r) ? init : op(init, $fred(r))
end
end
4 changes: 2 additions & 2 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1554,8 +1554,8 @@ end
(range(10, stop=20, length=5), 1, 5),
(range(10.3, step=-2, length=7), 7, 1),
]
@test minimum(r) === r[imin]
@test maximum(r) === r[imax]
@test minimum(r) === minimum(r, init=typemax(eltype(r))) === r[imin]
@test maximum(r) === maximum(r, init=typemin(eltype(r))) === r[imax]
@test imin === argmin(r)
@test imax === argmax(r)
@test extrema(r) === (r[imin], r[imax])
Expand Down
Loading