diff --git a/README.md b/README.md
index 812f5b9d6..a5f5ca6d4 100644
--- a/README.md
+++ b/README.md
@@ -229,7 +229,9 @@ average(1, 2, 3);
* [`isEven`](#iseven)
* [`isPrime`](#isprime)
* [`lcm`](#lcm)
+* [`maxN`](#maxn)
* [`median`](#median)
+* [`minN`](#minn)
* [`percentile`](#percentile)
* [`powerset`](#powerset)
* [`primes`](#primes)
@@ -340,19 +342,29 @@ average(1, 2, 3);
+### _Uncategorized_
+
+View contents
+
+* [`maxN`](#maxn)
+* [`minN`](#minn)
+
+Examples
@@ -364,23 +376,23 @@ const map = call.bind(null, 'map');
Promise.resolve([1, 2, 3])
.then(map(x => 2 * x))
.then(console.log); //[ 2, 4, 6 ]
-```
+```
[⬆ Back to top](#table-of-contents)
-### collectInto
-
-Changes a function that accepts an array into a variadic function.
-
-Given a function, return a closure that collects all inputs into an array-accepting function.
-
+### collectInto
+
+Changes a function that accepts an array into a variadic function.
+
+Given a function, return a closure that collects all inputs into an array-accepting function.
+
```js
const collectInto = fn => (...args) => fn(args);
-```
-
+```
+
Examples
@@ -390,23 +402,23 @@ let p1 = Promise.resolve(1);
let p2 = Promise.resolve(2);
let p3 = new Promise(resolve => setTimeout(resolve, 2000, 3));
Pall(p1, p2, p3).then(console.log);
-```
+```
[⬆ Back to top](#table-of-contents)
-### flip
-
-Flip takes a function as an argument, then makes the first argument the last
-
-Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
-
+### flip
+
+Flip takes a function as an argument, then makes the first argument the last
+
+Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
+
```js
const flip = fn => (...args) => fn(args.pop(), ...args);
-```
-
+```
+
Examples
@@ -418,7 +430,7 @@ let mergePerson = mergeFrom.bind(null, a);
mergePerson(b); // == b
b = {};
Object.assign(b, a); // == b
-```
+```
[⬆ Back to top](#table-of-contents)
-### spreadOver
-
-Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function.
-
-Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function.
-
+### spreadOver
+
+Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function.
+
+Use closures and the spread operator (`...`) to map the array of arguments to the inputs of the function.
+
```js
const spreadOver = fn => argsArr => fn(...argsArr);
-```
-
+```
+
Examples
@@ -497,7 +509,7 @@ const spreadOver = fn => argsArr => fn(...argsArr);
const arrayMax = spreadOver(Math.max);
arrayMax([1, 2, 3]); // 3
arrayMax([1, 2, 4]); // 4
-```
+```
[⬆ Back to top](#table-of-contents)
+### maxN
+
+Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order).
+
+Sort's the array's shallow copy in descending order and returns the first n elements
+
+Skip the second argument to get a single element(in the form of a array)
+```js
+const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
+```
+
+Examples
+
+```js
+maxN([1, 2, 3]); // [3]
+maxN([1, 2, 3], 2); // [3,2]
+maxN([1, 2, 3], 4); // [3,2,1]
+```
+
+
[⬆ Back to top](#table-of-contents)
+
+
### median
Returns the median of an array of numbers.
@@ -3299,6 +3336,30 @@ median([0, 10, -2, 7]); // 3.5
[⬆ Back to top](#table-of-contents)
+### minN
+
+Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
+
+Sort's the array's shallow copy in ascending order and returns the first n elements
+
+Skip the second argument to get a single element(in the form of a array)
+```js
+const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
+```
+Examples
+
+```js
+minN([1, 2, 3]); // [1]
+minN([1, 2, 3], 2); // [1,2]
+minN([1, 2, 3], 4); // [1,2,3]
+```
+
+
[⬆ Back to top](#table-of-contents)
+
+
### percentile
Uses the percentile formula to calculate how many numbers in the given array are less or equal to the given value.
@@ -5349,6 +5410,45 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents)
+### maxN
+
+Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order).
+
+Sort's the array's shallow copy in descending order and returns the first n elements
+
+Skip the second argument to get a single element(in the form of a array)
+```js
+const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
+```
+
+```js
+maxN([1, 2, 3]); // [3]
+maxN([1, 2, 3], 2); // [3,2]
+maxN([1, 2, 3], 4); // [3,2,1]
+```
+
+
[⬆ back to top](#table-of-contents)
+
+
+### minN
+
+Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
+
+Sort's the array's shallow copy in ascending order and returns the first n elements
+
+Skip the second argument to get a single element(in the form of a array)
+```js
+const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
+```
+```js
+minN([1, 2, 3]); // [1]
+minN([1, 2, 3], 2); // [1,2]
+minN([1, 2, 3], 4); // [1,2,3]
+```
+
+
[⬆ back to top](#table-of-contents)
+
+
## Collaborators
| [
](https://github.com/Chalarangelo)
[Angelos Chalaris](https://github.com/Chalarangelo) | [
](https://github.com/Pl4gue)
[David Wu](https://github.com/Pl4gue) | [
](https://github.com/fejes713)
[Stefan Feješ](https://github.com/fejes713) | [
](https://github.com/kingdavidmartins)
[King David Martins](https://github.com/iamsoorena) | [
](https://github.com/iamsoorena)
[Soorena Soleimani](https://github.com/iamsoorena) |
diff --git a/docs/index.html b/docs/index.html
index cd1710c9c..1ef56d4ff 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -717,6 +717,10 @@ isPrime(12); // false
};
lcm(12, 7); // 84
lcm([1, 3, 4], 5); // 60
+Returns the n maximum elements from the provided array. If n is greater than or equal to the provided array's length than return the original array(sorted in descending order).
Sort's the array's shallow copy in descending order and returns the first n elements
Skip the second argument to get a single element(in the form of a array)
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
+maxN([1, 2, 3]); // [3]
+maxN([1, 2, 3], 2); // [3,2]
+maxN([1, 2, 3], 4); // [3,2,1]
Returns the median of an array of numbers.
Find the middle of the array, use Array.sort() to sort the values. Return the number at the midpoint if length is odd, otherwise the average of the two middle numbers.
const median = arr => {
const mid = Math.floor(arr.length / 2),
nums = [...arr].sort((a, b) => a - b);
@@ -724,6 +728,10 @@ lcm([1, 3, 4], 5); // 60
};
median([5, 6, 50, 1, -5]); // 5
median([0, 10, -2, 7]); // 3.5
+Returns the n minimum elements from the provided array. If n is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
Sort's the array's shallow copy in ascending order and returns the first n elements
Skip the second argument to get a single element(in the form of a array)
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
+minN([1, 2, 3]); // [1]
+minN([1, 2, 3], 2); // [1,2]
+minN([1, 2, 3], 4); // [1,2,3]
Uses the percentile formula to calculate how many numbers in the given array are less or equal to the given value.
Use Array.reduce() to calculate how many numbers are below the value and how many are the same value and apply the percentile formula.
const percentile = (arr, val) =>
100 * arr.reduce((acc, v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length;
percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 6); // 55
diff --git a/snippets/memoize.md b/snippets/memoize.md
index c1b6b1343..856375a69 100644
--- a/snippets/memoize.md
+++ b/snippets/memoize.md
@@ -2,13 +2,20 @@
Returns the memoized (cached) function.
-Use `Object.create(null)` to create an empty object without `Object.prototype` (so that those properties are not resolved if the input value is something like `'hasOwnProperty'`).
-Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not.
+Create an empty cache by instantiating a new `Map` object.
+Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. The `function` keyword must be used in order to allow the memoized function to have its `this` context changed if necessary.
+Allow access to the `cache` by setting it as a property on the returned function.
```js
const memoize = fn => {
- const cache = Object.create(null);
- return value => cache[value] || (cache[value] = fn(value));
+ const cache = new Map();
+ const cached = function(val) {
+ return cache.has(val)
+ ? cache.get(val)
+ : cache.set(val, fn.call(this, val)) && cache.get(val);
+ };
+ cached.cache = cache;
+ return cached;
};
```
@@ -17,4 +24,5 @@ const memoize = fn => {
const anagramsCached = memoize(anagrams);
anagramsCached('javascript'); // takes a long time
anagramsCached('javascript'); // returns virtually instantly since it's now cached
+console.log(anagramsCached.cache); // Map
```