diff --git a/README.md b/README.md index a5f5ca6d4..68772cd5a 100644 --- a/README.md +++ b/README.md @@ -229,9 +229,7 @@ average(1, 2, 3); * [`isEven`](#iseven) * [`isPrime`](#isprime) * [`lcm`](#lcm) -* [`maxN`](#maxn) * [`median`](#median) -* [`minN`](#minn) * [`percentile`](#percentile) * [`powerset`](#powerset) * [`primes`](#primes) @@ -342,29 +340,19 @@ average(1, 2, 3); -### _Uncategorized_ - -
-View contents - -* [`maxN`](#maxn) -* [`minN`](#minn) - -
- --- ## 🔌 Adapter -### call - -Given a key and a set of arguments, call them when given a context. Primarily useful in composition. - -Use a closure to call a stored key with stored arguments. - +### call + +Given a key and a set of arguments, call them when given a context. Primarily useful in composition. + +Use a closure to call a stored key with stored arguments. + ```js const call = (key, ...args) => context => context[key](...args); -``` - +``` +
Examples @@ -376,23 +364,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 @@ -402,23 +390,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 @@ -430,7 +418,7 @@ let mergePerson = mergeFrom.bind(null, a); mergePerson(b); // == b b = {}; Object.assign(b, a); // == b -``` +```
@@ -492,16 +480,16 @@ delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s
[⬆ 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 @@ -509,7 +497,7 @@ const spreadOver = fn => argsArr => fn(...argsArr); const arrayMax = spreadOver(Math.max); arrayMax([1, 2, 3]); // 3 arrayMax([1, 2, 4]); // 4 -``` +```
@@ -2556,13 +2544,18 @@ functionName(Math.max); // max (logged in debug channel of console) 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; }; ``` @@ -2574,6 +2567,7 @@ 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 ``` @@ -3283,31 +3277,6 @@ lcm([1, 3, 4], 5); // 60
[⬆ 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. @@ -3336,30 +3305,6 @@ 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. @@ -5410,45 +5355,6 @@ 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 1ef56d4ff..56de24d69 100644 --- a/docs/index.html +++ b/docs/index.html @@ -546,14 +546,19 @@ longRunningFunction(); // the browser will not update the HTML until this has fi defer(longRunningFunction); // the browser will update the HTML then run the function

functionName

Logs the name of a function.

Use console.debug() and the name property of the passed method to log the method's name to the debug channel of the console.

const functionName = fn => (console.debug(fn.name), fn);
 
functionName(Math.max); // max (logged in debug channel of console)
-

memoize

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.

const memoize = fn => {
-  const cache = Object.create(null);
-  return value => cache[value] || (cache[value] = fn(value));
+

memoize

Returns the memoized (cached) function.

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.

const memoize = fn => {
+  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;
 };
 
// See the `anagrams` snippet.
 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
 

once

Ensures a function is called only once.

Utilizing a closure, use a flag, called, and set it to true once the function is called for the first time, preventing it from being called again. In order to allow the function to have its this context changed (such as in an event listener), the function keyword must be used, and the supplied function must have the context applied. Allow the function to be supplied with an arbitrary number of arguments using the rest/spread (...) operator.

const once = fn => {
   let called = false;
   return function(...args) {
@@ -717,10 +722,6 @@ isPrime(12); // false
 };
 
lcm(12, 7); // 84
 lcm([1, 3, 4], 5); // 60
-

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)

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]
 

median

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);
@@ -728,10 +729,6 @@ maxN([1, 2, 3], 4); // [3,2,1]
 };
 
median([5, 6, 50, 1, -5]); // 5
 median([0, 10, -2, 7]); // 3.5
-

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)

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]
 

percentile

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 856375a69..fe753f8c8 100644
--- a/snippets/memoize.md
+++ b/snippets/memoize.md
@@ -10,9 +10,7 @@ Allow access to the `cache` by setting it as a property on the returned function
 const memoize = fn => {
   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);
+    return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val);
   };
   cached.cache = cache;
   return cached;