-
-
Notifications
You must be signed in to change notification settings - Fork 746
Introduce "fold" as an alternative to "reduce" #3968
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
Conversation
c746e23
to
72eab7f
Compare
I believe this is just historical baggage. Today it would be written as |
std/algorithm/iteration.d
Outdated
Lazily computes all permutations using Heap's algorithm.) | ||
$(T2 reduce, | ||
$(D reduce!((a, b) => a + b)([1, 2, 3, 4])) returns $(D 10).) | ||
$(T2 fold, |
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.
Don't break the lexical order.
Changelog entry is missing. |
For a smoother deprecation path, it will be better to use |
The reference in |
I don't think it's a good idea to use fold as the "real" implementation. It's a lot more work for little to no benefit, and contrary to what was discussed here. |
assert(arr.fold!(min, max) == tuple(1, 5)); | ||
|
||
// Compute minimum and maximum at the same time with seeds | ||
assert(arr.fold!(min, max)(0, 7) == tuple(0, 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.
What about adding an example of a UFCS chain, which is the primary motivation for introducing this function over reduce
in the first place?
Looks like a rebase is in order. |
Added UFCS example and rebased. |
std/algorithm/iteration.d
Outdated
assert(arr.fold!(min, max)(0, 7) == tuple(0, 7)); | ||
|
||
// Can be used in a UFCS chain | ||
assert(arr.map!(a => a +1).fold!((a, b) => a + b) == 20); |
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.
Nitpick: insert space after +
, i.e., + 1
instead of +1
.
Also, please add |
Nice! I came by this issue from #1955 and thought this will never make it into phobos. Thanks @atilaneves! I would recommend to add some comments to |
I'm on holiday right now, will address the most recent comments when I get back home |
Comments addressed. |
Thanks! LGTM. |
ping random reviewers @JakobOvrum @andralex @yebblies @klickverbot |
|
||
|
||
/++ | ||
Implements the homonym function (also known as $(D accumulate), $(D |
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.
Would be nice to use backticks instead of $(D ...)
throughout (not a blocker).
This looks good. Will pull as is but please there is one follow-up task for @atilaneves: please make one more pass through all documentation and replace all uses of |
Auto-merge toggled on |
Introduce "fold" as an alternative to "reduce"
I took the easy way out here; I have no idea why
reduce
throws when passed an empty range and I don't thinkfold
should do it either. However, since it's a lot harder to makefold
nothrow
I'll leave it as it is right now and maybe submit a PR later to make it so. IMHO, this should compile and pass:i.e. return
Unqual!(ElementType!R).init
when passed in an empty range. It gets more complicated sincereduce
also supportsopApply
.