-
Notifications
You must be signed in to change notification settings - Fork 189
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
d3.cumsum? #102
Comments
I think I would implement this as function cumsum(values) {
let sum = 0;
return Float64Array.from(values, v => sum += v);
} so that it works with iterables. But I’d also change it to coerce to numbers, treat NaN as zero, and take an accessor. |
function cumsum(values, valueof) {
var sum = 0, index = 0, value;
return Float64Array.from(values, valueof === undefined
? v => (sum += +v || 0)
: v => (sum += +valueof(v, index++, values) || 0));
} (edited with Mike's suggestion) |
Merged
Documentation: https://observablehq.com/d/918b60f6963a81e1 |
Added in 2.4. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taking a cumulative sum is a common function in numeric computing and I've found myself repeatedly reaching for it over the years.
It can be implemented in one line, though you can achieve better performance by copying the array and running through
1:n
addinga[n-1]
toa[n]
inside each iteration.The text was updated successfully, but these errors were encountered: