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

use Iterators.reverse for foldr etc. #31781

Merged
merged 9 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 13 additions & 21 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mul_prod(x::Real, y::Real)::Real = x * y

## foldl && mapfoldl

@noinline function mapfoldl_impl(f, op, nt::NamedTuple{(:init,)}, itr, i...)
function mapfoldl_impl(f, op, nt::NamedTuple{(:init,)}, itr, i...)
init = nt.init
# Unroll the while loop once; if init is known, the call to op may
# be evaluated at compile time
Expand Down Expand Up @@ -91,28 +91,20 @@ foldl(op, itr; kw...) = mapfoldl(identity, op, itr; kw...)

## foldr & mapfoldr

function mapfoldr_impl(f, op, nt::NamedTuple{(:init,)}, itr, i::Integer)
init = nt.init
# Unroll the while loop once; if init is known, the call to op may
# be evaluated at compile time
if isempty(itr) || i == 0
return init
else
x = itr[i]
v = op(f(x), init)
while i > 1
x = itr[i -= 1]
v = op(f(x), v)
end
return v
end
end
mapfoldr_impl(f, op, nt::NamedTuple{(:init,)}, ritr, i...) =
mapfoldl_impl(f, (x,y) -> op(y,x), nt, ritr, i...)

function mapfoldr_impl(f, op, ::NamedTuple{()}, itr, i::Integer)
if isempty(itr)
# we can't just call mapfoldl_impl with (x,y) -> op(y,x), because
# we need to use the type of op for mapreduce_empty_iter and mapreduce_first.
function mapfoldr_impl(f, op, nt::NamedTuple{()}, itr)
ritr = Iterators.reverse(itr)
y = iterate(ritr)
if y === nothing
return Base.mapreduce_empty_iter(f, op, itr, IteratorEltype(itr))
end
return mapfoldr_impl(f, op, (init=mapreduce_first(f, op, itr[i]),), itr, i-1)
(x, i) = y
stevengj marked this conversation as resolved.
Show resolved Hide resolved
init = mapreduce_first(f, op, x)
return mapfoldr_impl(f, op, (init=init,), ritr, i)
end

"""
Expand All @@ -122,7 +114,7 @@ Like [`mapreduce`](@ref), but with guaranteed right associativity, as in [`foldr
provided, the keyword argument `init` will be used exactly once. In general, it will be
necessary to provide `init` to work with empty collections.
"""
mapfoldr(f, op, itr; kw...) = mapfoldr_impl(f, op, kw.data, itr, lastindex(itr))
mapfoldr(f, op, itr; kw...) = mapfoldr_impl(f, op, kw.data, itr)


"""
Expand Down
2 changes: 2 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ using .Main.OffsetArrays
@test Base.mapfoldr(abs2, -, 2:5) == -14
@test Base.mapfoldr(abs2, -, 2:5; init=10) == -4

@test foldr((x, y) -> ('⟨' * x * '|' * y * '⟩'), "λ 🐨.α") == "⟨λ|⟨ |⟨🐨|⟨.|α⟩⟩⟩⟩" # issue #31780

stevengj marked this conversation as resolved.
Show resolved Hide resolved
# reduce
@test reduce(+, Int64[]) === Int64(0) # In reference to issue #20144 (PR #20160)
@test reduce(+, Int16[]) === Int16(0) # In reference to issues #21536
Expand Down