Merge branch 'master' into add-permuteAll
This commit is contained in:
15
snippets/all.md
Normal file
15
snippets/all.md
Normal file
@ -0,0 +1,15 @@
|
||||
### all
|
||||
|
||||
Returns `true` if the provided predicate function returns `true` for all elements in a collection, `false` otherwise.
|
||||
|
||||
Use `Array.every()` to test if all elements in the collection return `true` based on `fn`.
|
||||
Omit the second argument, `fn`, to use `Boolean` as a default.
|
||||
|
||||
```js
|
||||
const all = (arr, fn = Boolean) => arr.every(fn);
|
||||
```
|
||||
|
||||
```js
|
||||
all([4, 2, 3], x => x > 1); // true
|
||||
all([1, 2, 3]); // true
|
||||
```
|
||||
15
snippets/any.md
Normal file
15
snippets/any.md
Normal file
@ -0,0 +1,15 @@
|
||||
### any
|
||||
|
||||
Returns `true` if the provided predicate function returns `true` for at least one element in a collection, `false` otherwise.
|
||||
|
||||
Use `Array.some()` to test if any elements in the collection return `true` based on `fn`.
|
||||
Omit the second argument, `fn`, to use `Boolean` as a default.
|
||||
|
||||
```js
|
||||
const any = (arr, fn = Boolean) => arr.some(fn);
|
||||
```
|
||||
|
||||
```js
|
||||
any([0, 1, 2, 0], x => x >= 2); // true
|
||||
any([0, 0, 1, 0]); // true
|
||||
```
|
||||
14
snippets/approximatelyEqual.md
Normal file
14
snippets/approximatelyEqual.md
Normal file
@ -0,0 +1,14 @@
|
||||
### approximatelyEqual
|
||||
|
||||
Checks if two numbers are approximately equal to each other.
|
||||
|
||||
Use `Math.abs()` to compare the absolute difference of the two values to `epsilon`.
|
||||
Omit the third parameter, `epsilon`, to use a default value of `0.001`.
|
||||
|
||||
```js
|
||||
const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
|
||||
```
|
||||
|
||||
```js
|
||||
approximatelyEqual(Math.PI / 2.0, 1.5708); // true
|
||||
```
|
||||
14
snippets/bifurcate.md
Normal file
14
snippets/bifurcate.md
Normal file
@ -0,0 +1,14 @@
|
||||
### bifurcate
|
||||
|
||||
Splits values into two groups. If an element in `filter` is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
|
||||
|
||||
Use `Array.reduce()` and `Array.push()` to add elements to groups, based on `filter`.
|
||||
|
||||
```js
|
||||
const bifurcate = (arr, filter) =>
|
||||
arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);
|
||||
```
|
||||
|
||||
```js
|
||||
bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ]
|
||||
```
|
||||
14
snippets/bifurcateBy.md
Normal file
14
snippets/bifurcateBy.md
Normal file
@ -0,0 +1,14 @@
|
||||
### bifurcateBy
|
||||
|
||||
Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group.
|
||||
|
||||
Use `Array.reduce()` and `Array.push()` to add elements to groups, based on the value returned by `fn` for each element.
|
||||
|
||||
```js
|
||||
const bifurcateBy = (arr, fn) =>
|
||||
arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);
|
||||
```
|
||||
|
||||
```js
|
||||
bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b'); // [ ['beep', 'boop', 'bar'], ['foo'] ]
|
||||
```
|
||||
26
snippets/binomialCoefficient.md
Normal file
26
snippets/binomialCoefficient.md
Normal file
@ -0,0 +1,26 @@
|
||||
### binomialCoefficient
|
||||
|
||||
Evaluates the binomial coefficient of two integers `n` and `k`.
|
||||
|
||||
Use `Number.isNaN()` to check if any of the two values is `NaN`.
|
||||
Check if `k` is less than `0`, greater than or equal to `n`, equal to `1` or `n - 1` and return the appropriate result.
|
||||
Check if `n - k` is less than `k` and switch their values accordingly.
|
||||
Loop from `2` through `k` and calculate the binomial coefficient.
|
||||
Use `Math.round()` to account for rounding errors in the calculation.
|
||||
|
||||
```js
|
||||
const binomialCoefficient = (n, k) => {
|
||||
if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
|
||||
if (k < 0 || k > n) return 0;
|
||||
if (k === 0 || k === n) return 1;
|
||||
if (k === 1 || k === n - 1) return n;
|
||||
if (n - k < k) k = n - k;
|
||||
let res = n;
|
||||
for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
|
||||
return Math.round(res);
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
binomialCoefficient(8, 2); // 28
|
||||
```
|
||||
13
snippets/degreesToRads.md
Normal file
13
snippets/degreesToRads.md
Normal file
@ -0,0 +1,13 @@
|
||||
### degreesToRads
|
||||
|
||||
Converts an angle from degrees to radians.
|
||||
|
||||
Use `Math.PI` and the degree to radian formula to convert the angle from degrees to radians.
|
||||
|
||||
```js
|
||||
const degreesToRads = deg => deg * Math.PI / 180.0;
|
||||
```
|
||||
|
||||
```js
|
||||
degreesToRads(90.0); // ~1.5708
|
||||
```
|
||||
31
snippets/mostPerformant.md
Normal file
31
snippets/mostPerformant.md
Normal file
@ -0,0 +1,31 @@
|
||||
### mostPerformant
|
||||
|
||||
Returns the index of the function in an array of functions which executed the fastest.
|
||||
|
||||
Use `Array.map()` to generate an array where each value is the total time taken to execute the function after `iterations` times. Use the difference in `performance.now()` values before and after to get the total time in milliseconds to a high degree of accuracy.
|
||||
Use `Math.min()` to find the minimum execution time, and return the index of that shortest time which corresponds to the index of the most performant function.
|
||||
Omit the second argument, `iterations`, to use a default of 10,000 iterations. The more iterations, the more reliable the result but the longer it will take.
|
||||
|
||||
```js
|
||||
const mostPerformant = (fns, iterations = 10000) => {
|
||||
const times = fns.map(fn => {
|
||||
const before = performance.now();
|
||||
for (let i = 0; i < iterations; i++) fn();
|
||||
return performance.now() - before;
|
||||
});
|
||||
return times.indexOf(Math.min(...times));
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
mostPerformant([
|
||||
() => {
|
||||
// Loops through the entire array before returning `false`
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, '10'].every(el => typeof el === 'number');
|
||||
},
|
||||
() => {
|
||||
// Only needs to reach index `1` before returning false
|
||||
[1, '2', 3, 4, 5, 6, 7, 8, 9, 10].every(el => typeof el === 'number');
|
||||
}
|
||||
]); // 1
|
||||
```
|
||||
15
snippets/none.md
Normal file
15
snippets/none.md
Normal file
@ -0,0 +1,15 @@
|
||||
### none
|
||||
|
||||
Returns `true` if the provided predicate function returns `false` for all elements in a collection, `false` otherwise.
|
||||
|
||||
Use `Array.some()` to test if any elements in the collection return `true` based on `fn`.
|
||||
Omit the second argument, `fn`, to use `Boolean` as a default.
|
||||
|
||||
```js
|
||||
const none = (arr, fn = Boolean) => !arr.some(fn);
|
||||
```
|
||||
|
||||
```js
|
||||
none([0, 1, 3, 0], x => x == 2); // true
|
||||
none([0, 0, 0]); // true
|
||||
```
|
||||
13
snippets/radsToDegrees.md
Normal file
13
snippets/radsToDegrees.md
Normal file
@ -0,0 +1,13 @@
|
||||
### radsToDegrees
|
||||
|
||||
Converts an angle from radians to degrees.
|
||||
|
||||
Use `Math.PI` and the radian to degree formula to convert the angle from radians to degrees.
|
||||
|
||||
```js
|
||||
const radsToDegrees = rad => rad * 180.0 / Math.PI;
|
||||
```
|
||||
|
||||
```js
|
||||
radsToDegrees(Math.PI / 2); // 90
|
||||
```
|
||||
21
snippets/stableSort.md
Normal file
21
snippets/stableSort.md
Normal file
@ -0,0 +1,21 @@
|
||||
### stableSort
|
||||
|
||||
Performs stable sorting of an array, preserving the initial indexes of items when their values are the same.
|
||||
Does not mutate the original array, but returns a new array instead.
|
||||
|
||||
Use `Array.map()` to pair each element of the input array with its corresponding index.
|
||||
Use `Array.sort()` and a `compare` function to sort the list, preserving their initial order if the items compared are equal.
|
||||
Use `Array.map()` to convert back to the initial array items.
|
||||
|
||||
```js
|
||||
const stableSort = (arr, compare) =>
|
||||
arr
|
||||
.map((item, index) => ({ item, index }))
|
||||
.sort((a, b) => compare(a.item, b.item) || a.index - b.index)
|
||||
.map(({ item }) => item);
|
||||
```
|
||||
|
||||
```js
|
||||
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
const stable = stableSort(arr, () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
```
|
||||
23
snippets/uncurry.md
Normal file
23
snippets/uncurry.md
Normal file
@ -0,0 +1,23 @@
|
||||
### uncurry
|
||||
|
||||
Uncurries a function up to depth `n`.
|
||||
|
||||
Return a variadic function.
|
||||
Use `Array.reduce()` on the provided arguments to call each subsequent curry level of the function.
|
||||
If the `length` of the provided arguments is less than `n` throw an error.
|
||||
Otherwise, call `fn` with the proper amount of arguments, using `Array.slice(0, n)`.
|
||||
Omit the second argument, `n`, to uncurry up to depth `1`.
|
||||
|
||||
```js
|
||||
const uncurry = (fn, n = 1) => (...args) => {
|
||||
const next = acc => args => args.reduce((x, y) => x(y), acc);
|
||||
if (n > args.length) throw new RangeError('Arguments too few!');
|
||||
return next(fn)(args.slice(0, n));
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
const add = x => y => z => x + y + z;
|
||||
const uncurriedAdd = uncurry(add, 3);
|
||||
uncurriedAdd(1, 2, 3); // 6
|
||||
```
|
||||
Reference in New Issue
Block a user