Add std.algorithm.iteration.cumulativeFold#3972
Add std.algorithm.iteration.cumulativeFold#3972DmitryOlshansky merged 2 commits intodlang:masterfrom dcarp:std_algorithm_iteration_scan
Conversation
|
This seems to be a rather useful function, but |
|
Actually I find the name quite good. It is already used in haskell [1] and C++ [2] seams to adopt it also. |
|
Thanks for the contribution. I don't have bandwidth for it just now, but I'll note that the name does not do it justice. I'd already forgotten what the algorithm does (something with partial sums) and the name definitely didn't remind me of anything, maybe except... FoxPro :o). |
|
On wikipedia it is called "prefix sum", "scan" or "cumulative sum". Considering the generic implementation, I think that the name that does not contain the word "sum" is the better one :). |
|
What about |
std/algorithm/iteration.d
Outdated
| assert(sum2.array == [1, 3, 6, 10, 15]); | ||
|
|
||
| // Compute the maximum of all elements | ||
| auto largest = scan!(max)(arr); |
There was a problem hiding this comment.
Examples should use single-token template argument list instantiation shortcut: scan!max(arr)
edit:
Oh, this isn't actually an example unit test... was that intentional? It's full of the kind of redundancies I'd expect from an example and not a traditional unit test.
|
The implementation should propagate forward range capability and the tests should test more kinds of ranges than just slices; see std.internal.test.dummyrange for helpers. |
changelog.dd
Outdated
|
|
||
| $(BUGSTITLE Library Changes, | ||
|
|
||
| $(LI $(LNAME2 std-algorithm-iteration-scan, $(XREF algorithm.iterator, scan) |
There was a problem hiding this comment.
XREF doesn't work like this. Please use $(REF scan, std, algorithm, iteration)
|
I think the documentation would be better if the overloads were consolidated in one place with |
|
@quickfur: considering that the function is called on each iteration, I think that a "verb" name is more appropriate. Moreover |
|
I suggest we call this something with fold in the name (assuming we're committed to renaming |
|
I vote for |
|
@greenify I like that name. Thanks! |
|
|
|
That works too. |
|
|
| else | ||
| alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns); | ||
|
|
||
| foreach (i, f; binfuns) |
There was a problem hiding this comment.
@andralex said once on a review of one of my PRs that foreach shouldn't be used for generic code.
There was a problem hiding this comment.
I'm not sure that I agree that foreach shouldn't be used in generic code. Sometimes, it's exactly what's appropriate. It's just that there's often a better way to do it that doesn't involve foreach, and you have to be careful with the element type, since foreach will infer strings to have their code unit as their element type and not dchar.
Regardless, in this case, it looks like this is a static foreach (binfuns is the result of staticMap), so it pretty much has to be done either with a foreach or a recursive template. The foreach is likely to be cleaner and more efficient, particularly when this is inside of a function rather than something that's part of the public API.
There was a problem hiding this comment.
This is static, there's no other way.
|
@quickfur: is |
|
cumulativeFold sounds about right to me |
|
@andralex are you ok with |
|
Needs the @andralex tag for name review! |
|
Ping @andralex |
|
I just accidentally ended up implementing this myself, because I forgot about the PR and obviously there were no docs yet :/ Btw in Scala this is called |
|
Actually the original name of this function was |
Please don't, there's hope: |
I prefer that, it's like STL's |
std/algorithm/iteration.d
Outdated
| it returns the first element unchanged and uses it as seed for the next | ||
| elements. | ||
| This function is also known as `partial_sum`, `accumulate`, `prefix_sum`, | ||
| `scan`, or `cumulative_sum`. |
There was a problem hiding this comment.
Making some or all of these names hot links would be great.
|
Thanks for this solid piece of work. I approve the addition, please pull once the nits are looked at. |
|
Thank you for the review! All comments were addressed. |
So we are good to go here? :) |
|
Auto-merge toggled on |
This PR implements
std.algorithm.iteration.scan.http://forum.dlang.org/post/n8gini$1ibc$1@digitalmars.com
http://en.cppreference.com/w/cpp/algorithm/partial_sum