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

Fix behavior of foldr on empty arrays (#20144) #20160

Merged
merged 6 commits into from
Jan 27, 2017
Merged
Changes from 2 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
8 changes: 7 additions & 1 deletion base/reduce.jl
Original file line number Diff line number Diff line change
@@ -131,7 +131,13 @@ mapfoldr(f, op, v0, itr) = mapfoldr_impl(f, op, v0, itr, endof(itr))
Like `mapfoldr(f, op, v0, itr)`, but using the first element of `itr` as `v0`. In general,
this cannot be used with empty collections (see `reduce(op, itr)`).
"""
mapfoldr(f, op, itr) = (i = endof(itr); mapfoldr_impl(f, op, f(itr[i]), itr, i-1))
function mapfoldr(f, op, itr)
i = endof(itr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the trailing semicolon; remove?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to remove it, sorry.
Removed.

if i == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I belive you don't need the i = endof(itr) here, instead, you can do if isempty(itr).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pabloferz, the existing implementation uses i in the call to mapfoldr_impl (see just below)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, missed that. Carry one then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is endof(itr) == 0 correct for all iterator types though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered the same given the mapfoldl implementation takes what might be a more general approach. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, start and done is definitively better and general enough (the endof(itr) == 0 is actually what threw me out)

Copy link
Author

@victhorio victhorio Jan 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the check from mapfoldr_impl (here) which tests if the i argument is 0, I'm assuming that if the one I implemented in mapfoldr should change so should that check?

return Base.mr_empty_iter(f, op, itr, iteratoreltype(itr))
end
return mapfoldr_impl(f, op, f(itr[i]), itr, i-1)
end

"""
foldr(op, v0, itr)
2 changes: 2 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

# fold(l|r) & mapfold(l|r)
@test foldl(+, Int64[]) == 0
@test foldl(-, 1:5) == -13
@test foldl(-, 10, 1:5) == -5

@@ -16,6 +17,7 @@
@test Base.mapfoldl((x)-> x true, |, [true false true false false]) == true
@test Base.mapfoldl((x)-> x true, |, false, [true false true false false]) == true

@test foldr(+, Int64[]) == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add a comment to each of these tests referencing the relevant issue and pull request?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the foldr test I can just reference issue #20144 and PR #20160 (this one), but on the foldl test I can only reference issue #7465 since there is no PR.
@Sacha0, do I just reference the relevant issue for the foldl test or do I add the SHA of the relevant commit? (b674eed)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referencing via the issue / pull request numbers (e.g. #20144, #20160) would be great. Given #20160 adds the foldl test and #20144/#20160 contain the relevant discussion, perhaps reference #20144 and #20160 for that test as well, rather than (or in addition to) #7465? Thanks!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Done.
Thanks for reviewing!

@test foldr(-, 1:5) == 3
@test foldr(-, 10, 1:5) == -7