Skip to content

Comments

Add std.algorithm.iteration.cumulativeFold#3972

Merged
DmitryOlshansky merged 2 commits intodlang:masterfrom
dcarp:std_algorithm_iteration_scan
Apr 29, 2016
Merged

Add std.algorithm.iteration.cumulativeFold#3972
DmitryOlshansky merged 2 commits intodlang:masterfrom
dcarp:std_algorithm_iteration_scan

Conversation

@dcarp
Copy link
Contributor

@dcarp dcarp commented Feb 4, 2016

@quickfur
Copy link
Member

quickfur commented Feb 5, 2016

This seems to be a rather useful function, but scan is a horrible name for it. My first reaction to seeing the name was, "isn't this already implemented as find, findUntil, or each?". We need a better name that reflects what the function does.

@dcarp
Copy link
Contributor Author

dcarp commented Feb 5, 2016

Actually I find the name quite good. It is already used in haskell [1] and C++ [2] seams to adopt it also.
[1] https://www.haskell.org/hoogle/?hoogle=scanl
[2] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4310.html#parallel.alg.inclusive.scan

@andralex
Copy link
Member

andralex commented Feb 7, 2016

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).

@dcarp
Copy link
Contributor Author

dcarp commented Feb 7, 2016

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 :).
Anyhow, if some good proposals are presented, I'm happy to change the name.

@quickfur
Copy link
Member

What about partials?

assert(sum2.array == [1, 3, 6, 10, 15]);

// Compute the maximum of all elements
auto largest = scan!(max)(arr);
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

@JakobOvrum
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

XREF doesn't work like this. Please use $(REF scan, std, algorithm, iteration)

@JakobOvrum
Copy link
Contributor

I think the documentation would be better if the overloads were consolidated in one place with /// Ditto like we've been encouraging as of late. In particular it allows eliminating redundant information across the board, improving the signal to noise ratio.

@dcarp
Copy link
Contributor Author

dcarp commented Feb 10, 2016

@quickfur: considering that the function is called on each iteration, I think that a "verb" name is more appropriate. Moreover partials can be easily confused with std.functional.partial.

@ntrel
Copy link
Contributor

ntrel commented Feb 29, 2016

I suggest we call this something with fold in the name (assuming we're committed to renaming reduce to fold). It's the same calculation as fold, just packaged differently. Maybe lazyFold, cumulativeFold, progressiveFold, successiveFold.

@wilzbach
Copy link
Contributor

I vote for cumulativeFold - imho the best name for this.

@quickfur
Copy link
Member

@greenify I like that name. Thanks!

@schuetzm
Copy link
Contributor

stepwiseFold?

@quickfur
Copy link
Member

That works too.

@dcarp
Copy link
Contributor Author

dcarp commented Feb 29, 2016

partialFold would work too...
At the moment I made the changes for cumulativeFold. If there are no strong opinions against it, I propose to go with it.

@dcarp dcarp changed the title Add std.algorithm.iteration.scan Add std.algorithm.iteration.cumulativeFold Mar 3, 2016
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);

foreach (i, f; binfuns)
Copy link
Contributor

Choose a reason for hiding this comment

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

@andralex said once on a review of one of my PRs that foreach shouldn't be used for generic code.

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

This is static, there's no other way.

@dcarp
Copy link
Contributor Author

dcarp commented Mar 9, 2016

@quickfur: is cumulativeFold name telling enough? Other open points?

@DmitryOlshansky
Copy link
Member

cumulativeFold sounds about right to me

@dcarp dcarp mentioned this pull request Mar 19, 2016
@dcarp
Copy link
Contributor Author

dcarp commented Mar 28, 2016

@andralex are you ok with cumulativeFold? Other proposed names were: stepwiseFold, partialFold...

@wilzbach
Copy link
Contributor

Needs the @andralex tag for name review!

@wilzbach
Copy link
Contributor

wilzbach commented Apr 8, 2016

Ping @andralex

@wilzbach
Copy link
Contributor

I just accidentally ended up implementing this myself, because I forgot about the PR and obviously there were no docs yet :/
-> +1 for moving forward with this PR.

Btw in Scala this is called scanLeft.
It would be great to get the "name ok" from @andralex for cumulativeFold.

@dcarp
Copy link
Contributor Author

dcarp commented Apr 25, 2016

Actually the original name of this function was scan (suggested by Timon Gehr), but this was more or less rejected as unrecognizable.
It is quite annoying that every week or so, I need to rebase this PR because of the change log entry and slowly I'm thinking about giving up on this :(.

@wilzbach
Copy link
Contributor

It is quite annoying that every week or so, I need to rebase this PR because of the change log entry and slowly I'm thinking about giving up on this :(.

Please don't, there's hope:
#4228

@ntrel
Copy link
Contributor

ntrel commented Apr 26, 2016

partialFold would work too...

I prefer that, it's like STL's partial_sum but more general.

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`.
Copy link
Member

Choose a reason for hiding this comment

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

Making some or all of these names hot links would be great.

@andralex
Copy link
Member

Thanks for this solid piece of work. I approve the addition, please pull once the nits are looked at.

@dcarp
Copy link
Contributor Author

dcarp commented Apr 27, 2016

Thank you for the review! All comments were addressed.

@wilzbach
Copy link
Contributor

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? :)

@DmitryOlshansky
Copy link
Member

Auto-merge toggled on

@DmitryOlshansky DmitryOlshansky merged commit 85f9e81 into dlang:master Apr 29, 2016
@dcarp dcarp deleted the std_algorithm_iteration_scan branch April 29, 2016 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants