Skip to content

Instantly share code, notes, and snippets.

@storuky
Last active March 25, 2019 14:55
Show Gist options
  • Save storuky/5550bfea2d8b6f3f3599d215aed682c2 to your computer and use it in GitHub Desktop.
Save storuky/5550bfea2d8b6f3f3599d215aed682c2 to your computer and use it in GitHub Desktop.
// С рекурсией (медленно)
const calcSum = list => list.reduce((acc, val) => acc + val, 0);
const cumulativeSum = (list, acc = []) => {
if (!list.length) return acc;
const sum = calcSum(list),
subset = list.slice(0, list.length - 1);
return cumulativeSum(subset, [sum, ...acc]);
}
// Без (быстро)
const cumulativeSum = list => list.reduce((acc, val) => {
const last = acc[acc.length-1] || 0;
return [...acc, val + last];
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment