diff --git a/README.md b/README.md index 60b481fa2..7bf7f2801 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ average(1, 2, 3); * [`pullAtIndex`](#pullatindex) * [`pullAtValue`](#pullatvalue) * [`reducedFilter`](#reducedfilter) +* [`reduceSuccessive`](#reducesuccessive) * [`remove`](#remove) * [`sample`](#sample) * [`sampleSize`](#samplesize) @@ -1693,6 +1694,29 @@ reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: '
[⬆ Back to top](#table-of-contents) +### 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]); +``` + +
+Examples + +```js +reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0); // [0, 1, 3, 6, 10, 15, 21] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### remove Removes elements from an array for which the given function returns `false`. diff --git a/docs/index.html b/docs/index.html index 69426fcb0..50aa785be 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -308,6 +308,9 @@ Object.assig
 ];
 
 reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: 'mike'}]
+

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.

const reduceSuccessive = (arr, fn, acc) =>
+  arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)), res), [acc]);
+
reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0); // [0, 1, 3, 6, 10, 15, 21]
 

remove

Removes elements from an array for which the given function returns false.

Use Array.filter() to find array elements that return truthy values and Array.reduce() to remove elements using Array.splice(). The func is invoked with three arguments (value, index, array).

const remove = (arr, func) =>
   Array.isArray(arr)
     ? arr.filter(func).reduce((acc, val) => {
diff --git a/snippets/reduceSuccessive.md b/snippets/reduceSuccessive.md
index ce7879696..dc4b154ed 100644
--- a/snippets/reduceSuccessive.md
+++ b/snippets/reduceSuccessive.md
@@ -6,9 +6,7 @@ Use `Array.reduce()` to apply the given function to the given array, storing eac
 
 ```js
 const reduceSuccessive = (arr, fn, acc) =>
-  arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)),res), [
-    acc,
-  ]);
+  arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)), res), [acc]);
 ```
 
 ```js