Performs left-to-right function composition.
Use Array.prototype.reduce() with the spread operator (...) to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))); diff --git a/docs/index.html b/docs/index.html index 202d4f792..3c08101c6 100644 --- a/docs/index.html +++ b/docs/index.html @@ -406,9 +406,9 @@
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); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : [];
remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4]
Returns a random element from an array.
Use Math.random() to generate a random number, multiply it by length and round it off to the nearest whole number using Math.floor(). This method also works with strings.
const sample = arr => arr[Math.floor(Math.random() * arr.length)]; @@ -423,7 +423,7 @@ };
sampleSize([1, 2, 3], 2); // [3,1] sampleSize([1, 2, 3], 4); // [2,3,1] -
Has the same functionality as Array.prototype.prototype.splice(), but returning a new array instead of mutating the original array.
Use Array.prototype.slice() and Array.prototype.concat() to get a new array with the new contents after removing existing elements and/or adding new elements. Omit the second argument, index, to start at 0. Omit the third argument, delCount, to remove 0 elements. Omit the fourth argument, elements, in order to not add any new elements.
const shank = (arr, index = 0, delCount = 0, ...elements) => +
Has the same functionality as Array.prototype.splice(), but returning a new array instead of mutating the original array.
Use Array.prototype.slice() and Array.prototype.concat() to get a new array with the new contents after removing existing elements and/or adding new elements. Omit the second argument, index, to start at 0. Omit the third argument, delCount, to remove 0 elements. Omit the fourth argument, elements, in order to not add any new elements.
const shank = (arr, index = 0, delCount = 0, ...elements) => arr .slice(0, index) .concat(elements) @@ -520,7 +520,7 @@ console.log< return arr; };
takeWhile([1, 2, 3, 4], n => n >= 3); // [1, 2] -
Reduces a given Array-like into a value hash (keyed data store).
Given an Iterable or Array-like structure, call Array.prototype.prototype.reduce.call() on the provided object to step over it and return an Object, keyed by the reference value.
const toHash = (object, key) => +
Reduces a given Array-like into a value hash (keyed data store).
Given an Iterable or Array-like structure, call Array.prototype.reduce.call() on the provided object to step over it and return an Object, keyed by the reference value.
const toHash = (object, key) => Array.prototype.reduce.call( object, (acc, data, index) => ((acc[!key ? index : data[key]] = data), acc), diff --git a/docs/math.html b/docs/math.html index eb33562eb..03bc22d01 100644 --- a/docs/math.html +++ b/docs/math.html @@ -150,8 +150,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 916164842..d49b31ba5 100644 --- a/docs/object.html +++ b/docs/object.html @@ -134,9 +134,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 8c93add3b..47d6f620c 100644 --- a/snippets/pipeAsyncFunctions.md +++ b/snippets/pipeAsyncFunctions.md @@ -17,7 +17,7 @@ const sum = pipeAsyncFunctions( x => x + 3, async x => (await x) + 4 ); -(async () => { +(async() => { console.log(await sum(5)); // 15 (after one second) })(); ``` diff --git a/snippets/remove.md b/snippets/remove.md index a8c472774..58de9c2e0 100644 --- a/snippets/remove.md +++ b/snippets/remove.md @@ -9,9 +9,9 @@ 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); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : []; ``` diff --git a/test/_30s.js b/test/_30s.js index 9cc89cd06..867e9f1c9 100644 --- a/test/_30s.js +++ b/test/_30s.js @@ -245,9 +245,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); @@ -319,8 +319,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); @@ -963,9 +963,9 @@ const reject = (pred, array) => array.filter((...args) => !pred(...args)); const remove = (arr, func) => Array.isArray(arr) ? arr.filter(func).reduce((acc, val) => { - arr.splice(arr.indexOf(val), 1); - return acc.concat(val); - }, []) + arr.splice(arr.indexOf(val), 1); + return acc.concat(val); + }, []) : []; const removeNonASCII = str => str.replace(/[^\x20-\x7E]/g, ''); const renameKeys = (keysMap, obj) =>