Last active
March 25, 2019 14:55
-
-
Save storuky/5550bfea2d8b6f3f3599d215aed682c2 to your computer and use it in GitHub Desktop.
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
// С рекурсией (медленно) | |
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