Add reduceSuccessive
Similar to Ramda's scan.
This commit is contained in:
16
snippets/reduceSuccessive.md
Normal file
16
snippets/reduceSuccessive.md
Normal 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]
|
||||||
|
```
|
||||||
@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user