Merge branch 'master' into any-all-none
This commit is contained in:
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
|
||||
```
|
||||
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
|
||||
```
|
||||
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