Performs left-to-right function composition for asynchronous functions.
Use Array.prototype.reduce() with the spread operator (...) to perform left-to-right function composition using Promise.then(). The functions can return a combination of: simple values, Promise's, or they can be defined as async ones returning through await. All functions must be unary.
const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg)); -
-const sum = pipeAsyncFunctions( +
const sum = pipeAsyncFunctions( x => x + 1, x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)), x => x + 3, diff --git a/docs/index.html b/docs/index.html index 73e0effb7..4b119a1d3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -407,8 +407,7 @@
Takes a predicate and array, like Array.prototype.filter(), but only keeps x if pred(x) === false.
const reject = (pred, array) => array.filter((...args) => !pred(...args));
reject(x => x % 2 === 0, [1, 2, 3, 4, 5]); // [1, 3, 5] reject(word => word.length > 4, ['Apple', 'Pear', 'Kiwi', 'Banana']); // ['Pear', 'Kiwi'] -
Removes elements from an array for which the given function returns false.
Use Array.prototype.filter() to find array elements that return truthy values and Array.prototype.reduce() to remove elements using Array.prototype.splice(). The func is invoked with three arguments (value, index, array).
-const remove = (arr, func) => +
Removes elements from an array for which the given function returns false.
Use Array.prototype.filter() to find array elements that return truthy values and Array.prototype.reduce() to remove elements using Array.prototype.splice(). The func is invoked with three arguments (value, index, array).
const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { arr.splice(arr.indexOf(val), 1); diff --git a/docs/math.html b/docs/math.html index f1a9bb838..ad21ae6e8 100644 --- a/docs/math.html +++ b/docs/math.html @@ -152,8 +152,8 @@ own individual rating by supplying it as the third argument.
Calculates the factorial of a number.
Use recursion. If n is less than or equal to 1, return 1. Otherwise, return the product of n and the factorial of n - 1. Throws an exception if n is a negative number.
const factorial = n => n < 0 ? (() => { - throw new TypeError('Negative numbers are not allowed!'); - })() + throw new TypeError('Negative numbers are not allowed!'); + })() : n <= 1 ? 1 : n * factorial(n - 1); diff --git a/docs/object.html b/docs/object.html index 0d1b6aed9..31acfc9ee 100644 --- a/docs/object.html +++ b/docs/object.html @@ -178,9 +178,9 @@ o[1in obj ? obj[target] : Object.values(obj).reduce((acc, val) => { - if (acc !== undefined) return acc; - if (typeof val === 'object') return dig(val, target); - }, undefined); + if (acc !== undefined) return acc; + if (typeof val === 'object') return dig(val, target); + }, undefined);
const data = { level1: { level2: { diff --git a/snippets/dig.md b/snippets/dig.md index c97559833..737130b6e 100644 --- a/snippets/dig.md +++ b/snippets/dig.md @@ -10,9 +10,9 @@ const dig = (obj, target) => target in obj ? obj[target] : Object.values(obj).reduce((acc, val) => { - if (acc !== undefined) return acc; - if (typeof val === 'object') return dig(val, target); - }, undefined); + if (acc !== undefined) return acc; + if (typeof val === 'object') return dig(val, target); + }, undefined); ``` ```js diff --git a/snippets/factorial.md b/snippets/factorial.md index 86ebe69b6..cf3acc9cc 100644 --- a/snippets/factorial.md +++ b/snippets/factorial.md @@ -11,8 +11,8 @@ Throws an exception if `n` is a negative number. const factorial = n => n < 0 ? (() => { - throw new TypeError('Negative numbers are not allowed!'); - })() + throw new TypeError('Negative numbers are not allowed!'); + })() : n <= 1 ? 1 : n * factorial(n - 1); diff --git a/snippets/pipeAsyncFunctions.md b/snippets/pipeAsyncFunctions.md index 61b850a39..47d6f620c 100644 --- a/snippets/pipeAsyncFunctions.md +++ b/snippets/pipeAsyncFunctions.md @@ -11,7 +11,6 @@ const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Pr ``` ```js - const sum = pipeAsyncFunctions( x => x + 1, x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)), diff --git a/snippets/remove.md b/snippets/remove.md index 1c457d2d9..58de9c2e0 100644 --- a/snippets/remove.md +++ b/snippets/remove.md @@ -6,7 +6,6 @@ Use `Array.prototype.filter()` to find array elements that return truthy values The `func` is invoked with three arguments (`value, index, array`). ```js - const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { diff --git a/test/_30s.js b/test/_30s.js index a0309377b..4dece075b 100644 --- a/test/_30s.js +++ b/test/_30s.js @@ -267,9 +267,9 @@ const dig = (obj, target) => target in obj ? obj[target] : Object.values(obj).reduce((acc, val) => { - if (acc !== undefined) return acc; - if (typeof val === 'object') return dig(val, target); - }, undefined); + if (acc !== undefined) return acc; + if (typeof val === 'object') return dig(val, target); + }, undefined); const digitize = n => [...`${n}`].map(i => parseInt(i)); const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0); const drop = (arr, n = 1) => arr.slice(n); @@ -341,8 +341,8 @@ const extendHex = shortHex => const factorial = n => n < 0 ? (() => { - throw new TypeError('Negative numbers are not allowed!'); - })() + throw new TypeError('Negative numbers are not allowed!'); + })() : n <= 1 ? 1 : n * factorial(n - 1); @@ -985,7 +985,6 @@ const reducedFilter = (data, keys, fn) => }, {}) ); const reject = (pred, array) => array.filter((...args) => !pred(...args)); - const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => {