diff --git a/snippets/reduceSuccessive.md b/snippets/reduceSuccessive.md new file mode 100644 index 000000000..ce7879696 --- /dev/null +++ b/snippets/reduceSuccessive.md @@ -0,0 +1,16 @@ +### reduceSuccessive + +Applies a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values. + +Use `Array.reduce()` to apply the given function to the given array, storing each new result. + +```js +const reduceSuccessive = (arr, fn, acc) => + arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)),res), [ + acc, + ]); +``` + +```js +reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0); // [0, 1, 3, 6, 10, 15, 21] +``` diff --git a/tag_database b/tag_database index b5ea61b91..9bd036292 100644 --- a/tag_database +++ b/tag_database @@ -182,6 +182,7 @@ randomNumberInRange:math,utility,random readFileLines:node,array,string redirect:browser,url reducedFilter:array +reduceSuccessive:array,function remove:array reverseString:string,array RGBToHex:utility