-
-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ $(T2 filter, | |
$(T2 filterBidirectional, | ||
Similar to $(D filter), but also provides $(D back) and $(D popBack) at | ||
a small increase in cost.) | ||
$(T2 fold, | ||
$(D fold!((a, b) => a + b)([1, 2, 3, 4])) returns $(D 10).) | ||
$(T2 group, | ||
$(D group([5, 2, 2, 3, 3])) returns a range containing the tuples | ||
$(D tuple(5, 1)), $(D tuple(2, 2)), and $(D tuple(3, 2)).) | ||
|
@@ -2520,6 +2522,8 @@ template reduce(fun...) if (fun.length >= 1) | |
$(D reduce) will operate on an unqualified copy. If this happens | ||
then the returned type will not perfectly match $(D S). | ||
|
||
Use $(D fold) instead of $(D reduce) to use the seed version in a UFCS chain. | ||
|
||
Params: | ||
fun = one or more functions | ||
seed = the initial value of the accumulator | ||
|
@@ -2868,6 +2872,80 @@ private template ReduceSeedType(E) | |
} | ||
} | ||
|
||
|
||
/++ | ||
Implements the homonym function (also known as $(D accumulate), $(D | ||
compress), $(D inject), or $(D foldl)) present in various programming | ||
languages of functional flavor. The call $(D fold!(fun)(range, seed)) | ||
first assigns $(D seed) to an internal variable $(D result), | ||
also called the accumulator. Then, for each element $(D x) in $(D | ||
range), $(D result = fun(result, x)) gets evaluated. Finally, $(D | ||
result) is returned. The one-argument version $(D fold!(fun)(range)) | ||
works similarly, but it uses the first element of the range as the | ||
seed (the range must be non-empty). | ||
|
||
Returns: | ||
the accumulated $(D result) | ||
|
||
See_Also: | ||
$(WEB en.wikipedia.org/wiki/Fold_(higher-order_function), Fold (higher-order function)) | ||
|
||
$(LREF sum) is similar to $(D fold!((a, b) => a + b)) that offers | ||
precise summing of floating point numbers. | ||
|
||
This is functionally equivalent to $(LREF reduce) with the argument order reversed, | ||
and without the need to use $(LREF tuple) for multiple seeds. | ||
+/ | ||
template fold(fun...) if (fun.length >= 1) | ||
{ | ||
auto fold(R, S...)(R r, S seed) | ||
{ | ||
static if (S.length < 2) | ||
{ | ||
return reduce!fun(seed, r); | ||
} | ||
else | ||
{ | ||
import std.typecons: tuple; | ||
return reduce!fun(tuple(seed), r); | ||
} | ||
} | ||
} | ||
|
||
/// | ||
@safe pure unittest | ||
{ | ||
immutable arr = [1, 2, 3, 4, 5]; | ||
|
||
// Sum all elements | ||
assert(arr.fold!((a, b) => a + b) == 15); | ||
|
||
// Sum all elements with explicit seed | ||
assert(arr.fold!((a, b) => a + b)(6) == 21); | ||
|
||
import std.algorithm.comparison: min, max; | ||
import std.typecons: tuple; | ||
|
||
// Compute minimum and maximum at the same time | ||
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 commentThe 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 |
||
|
||
// Can be used in a UFCS chain | ||
assert(arr.map!(a => a + 1).fold!((a, b) => a + b) == 20); | ||
} | ||
|
||
@safe @nogc pure nothrow unittest | ||
{ | ||
int[1] arr; | ||
static assert(!is(typeof(arr.fold!()))); | ||
static assert(!is(typeof(arr.fold!(a => a)))); | ||
static assert(is(typeof(arr.fold!((a, b) => a)))); | ||
static assert(is(typeof(arr.fold!((a, b) => a)(1)))); | ||
} | ||
|
||
|
||
// splitter | ||
/** | ||
Lazily splits a range using an element as a separator. This can be used with | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).