Merge branch 'master' into master

This commit is contained in:
Angelos Chalaris
2017-12-14 20:24:48 +02:00
committed by GitHub
27 changed files with 2089 additions and 878 deletions

View File

@ -1,6 +1,6 @@
### Array concatenation
Use `Array.concat()` to concatenate and array with any additional arrays and/or values, specified in `args`.
Use `Array.concat()` to concatenate an array with any additional arrays and/or values, specified in `args`.
```js
const arrayConcat = (arr, ...args) => arr.concat(...args);

View File

@ -3,6 +3,6 @@
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`.
```js
const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); }
const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); };
// difference([1,2,3], [1,2]) -> [3]
```

View File

@ -3,6 +3,6 @@
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`.
```js
const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); }
const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); };
// intersection([1,2,3], [4,3,2]) -> [2,3]
```

View File

@ -3,6 +3,6 @@
Create a `Set` with all values of `a` and `b` and convert to an array.
```js
const union = (a, b) => Array.from(new Set([...a, ...b]))
const union = (a, b) => Array.from(new Set([...a, ...b]));
// union([1,2,3], [4,3,2]) -> [1,2,3,4]
```

View File

@ -0,0 +1,9 @@
### Collatz algorithm
If `n` is even, return `n/2`. Otherwise return `3n+1`.
```js
const collatz = n => (n % 2 == 0) ? (n / 2) : (3 * n + 1);
// collatz(8) --> 4
// collatz(5) --> 16
```

View File

@ -6,13 +6,10 @@ Otherwise return a curried function `f` that expects the rest of the arguments.
If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. `Math.min()`), you can optionally pass the number of arguments to the second parameter `arity`.
```js
const curry = (f, arity = f.length, next) =>
(next = prevArgs =>
nextArg => {
const args = [ ...prevArgs, nextArg ];
return args.length >= arity ? f(...args) : next(args);
}
)([]);
const curry = (fn, arity = fn.length, ...args) =>
arity <= args.length
? fn(...args)
: curry.bind(null, fn, arity, ...args)
// curry(Math.pow)(2)(10) -> 1024
// curry(Math.min, 3)(10)(50)(2) -> 2
```

View File

@ -4,9 +4,9 @@ Loop through the array, using `Array.shift()` to drop the first element of the a
Returns the remaining elements.
```js
const dropElements = (arr,func) => {
while(arr.length > 0 && !func(arr[0])) arr.shift();
const dropElements = (arr, func) => {
while (arr.length > 0 && !func(arr[0])) arr.shift();
return arr;
}
};
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
```

View File

@ -12,7 +12,7 @@ const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
}
};
// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10}
// elementIsVisibleInViewport(el) -> false (not fully visible)
// elementIsVisibleInViewport(el, true) -> true (partially visible)

10
snippets/fill-array.md Normal file
View File

@ -0,0 +1,10 @@
### Fill array
Use `Array.map()` to map values between `start` (inclusive) and `end` (exclusive) to `value`.
Omit `start` to start at the first element and/or `end` to finish at the last.
```js
const fillArray = (arr, value, start = 0, end = arr.length) =>
arr.map((v, i) => i >= start && i < end ? value : v);
// fillArray([1,2,3,4],'8',1,3) -> [1,'8','8',4]
```

View File

@ -0,0 +1,13 @@
### Flatten array up to depth
Use recursion, decrementing `depth` by 1 for each level of depth.
Use `Array.reduce()` and `Array.concat()` to merge elements or arrays.
Base case, for `depth` equal to `1` stops recursion.
Omit the second element, `depth` to flatten only to a depth of `1` (single flatten).
```js
const flattenDepth = (arr, depth = 1) =>
depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
: arr.reduce((a, v) => a.concat(v), []);
// flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5]
```

View File

@ -5,10 +5,8 @@ Use `Array.reduce()` to create an object, where the keys are produced from the m
```js
const groupBy = (arr, func) =>
(typeof func === 'function' ? arr.map(func) : arr.map(val => val[func]))
.reduce((acc, val, i) => {
acc[val] = acc[val] === undefined ? [arr[i]] : acc[val].concat(arr[i]); return acc;
}, {});
arr.map(typeof func === 'function' ? func : val => val[func])
.reduce((acc, val, i) => { acc[val] = (acc[val] || []).concat(arr[i]); return acc; }, {});
// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
```

View File

@ -3,7 +3,7 @@
Use `Array.isArray()` to check if a value is classified as an array.
```js
const isArray = val => val && Array.isArray(val);
const isArray = val => !!val && Array.isArray(val);
// isArray(null) -> false
// isArray([1]) -> true
```

View File

@ -7,9 +7,9 @@ If digit is found in teens pattern, use teens ordinal.
```js
const toOrdinalSuffix = num => {
const int = parseInt(num), digits = [(int % 10), (int % 100)],
ordinals = ["st", "nd", "rd", "th"], oPattern = [1,2,3,4],
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19]
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0]-1] : int + ordinals[3];
}
ordinals = ['st', 'nd', 'rd', 'th'], oPattern = [1, 2, 3, 4],
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0] - 1] : int + ordinals[3];
};
// toOrdinalSuffix("123") -> "123rd"
```

View File

@ -11,7 +11,7 @@ const standardDeviation = (arr, usePopulation = false) => {
arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), [])
.reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1))
);
}
};
// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample)
// standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population)
```

View File

@ -4,7 +4,6 @@ Use `Array.slice()` to create a slice of the array with `n` elements taken from
```js
const take = (arr, n = 1) => arr.slice(0, n);
// take([1, 2, 3], 5) -> [1, 2, 3]
// take([1, 2, 3], 0) -> []
```