Last active
August 28, 2018 13:37
-
-
Save leolabs/2f385effb4bd5d8a3761b4f143f84aa9 to your computer and use it in GitHub Desktop.
Convert arrays from normal to cumulative and back
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
// [2, 4, 3, 8] => [2, 6, 9, 17] | |
const convertToCumulative = points => | |
points.reduce((acc, p, i) => acc.concat([ | |
p + (acc.length > 0 ? acc[i - 1] : 0) | |
]), []); | |
// [2, 6, 9, 17] => [2, 4, 3, 8] | |
const convertToNormal = points => | |
points.reduce((acc, p, i, ps) => acc.concat([ | |
p - (i > 0 ? ps[i - 1] : 0) | |
]), []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment