- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Changes from 2 commits
75ae5fb
2a06f9c
73bfee7
222810b
d3276ee
5abf3b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
if i == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I belive you don't need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pabloferz, the existing implementation uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, missed that. Carry one then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wondered the same given the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied the check from |
||
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) | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. Done. |
||
@test foldr(-, 1:5) == 3 | ||
@test foldr(-, 10, 1:5) == -7 | ||
|
||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.