Add reduceSuccessive

Similar to Ramda's scan.
This commit is contained in:
Angelos Chalaris
2018-01-24 16:38:08 +02:00
parent 65465791b4
commit 646bd79573
2 changed files with 17 additions and 0 deletions

View File

@ -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]
```

View File

@ -182,6 +182,7 @@ randomNumberInRange:math,utility,random
readFileLines:node,array,string readFileLines:node,array,string
redirect:browser,url redirect:browser,url
reducedFilter:array reducedFilter:array
reduceSuccessive:array,function
remove:array remove:array
reverseString:string,array reverseString:string,array
RGBToHex:utility RGBToHex:utility