Skip to content

d3.cumsum?Β #102

@yurivish

Description

@yurivish

Taking a cumulative sum is a common function in numeric computing and I've found myself repeatedly reaching for it over the years.

[1, 2, 3, 4].reduce((a, b) => (a.push(b + (a.length ? a[a.length-1] : 0)), a), [])

It can be implemented in one line, though you can achieve better performance by copying the array and running through 1:n adding a[n-1] to a[n] inside each iteration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions