Files
30-seconds-of-code/snippets/reduceSuccessive.md
Angelos Chalaris 646bd79573 Add reduceSuccessive
Similar to Ramda's scan.
2018-01-24 16:38:08 +02:00

17 lines
524 B
Markdown

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