From 983ed2ba297eb4222c9e2c75f0ee2ee69c4b8b9c Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 13:05:39 +1100 Subject: [PATCH 1/6] Update memoize to use a map and allow access to it --- snippets/memoize.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/snippets/memoize.md b/snippets/memoize.md index c1b6b1343..174812f26 100644 --- a/snippets/memoize.md +++ b/snippets/memoize.md @@ -2,13 +2,16 @@ 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'`). +Create an empty cache using 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. +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 = val => cache.get(val) || (cache.set(val, fn(val))) && cache.get(val); + cached.cache = cache; + return cached; }; ``` @@ -17,4 +20,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 ``` From 41ed3f5b0d0f16efa81f6559703d2a110acc7072 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 14:29:08 +1100 Subject: [PATCH 2/6] Update memoize.md --- snippets/memoize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/memoize.md b/snippets/memoize.md index 174812f26..f945b645d 100644 --- a/snippets/memoize.md +++ b/snippets/memoize.md @@ -2,7 +2,7 @@ Returns the memoized (cached) function. -Create an empty cache using by instantiating a new `Map` object. +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. Allow access to the `cache` by setting it as a property on the returned function. From 3c54e6a40214f50d19b5e55ec7c530d899b09f4e Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 2 Jan 2018 15:06:52 +1100 Subject: [PATCH 3/6] Update memoize.md --- snippets/memoize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/memoize.md b/snippets/memoize.md index f945b645d..7df16e542 100644 --- a/snippets/memoize.md +++ b/snippets/memoize.md @@ -9,7 +9,7 @@ Allow access to the `cache` by setting it as a property on the returned function ```js const memoize = fn => { const cache = new Map(); - const cached = val => cache.get(val) || (cache.set(val, fn(val))) && cache.get(val); + const cached = val => cache.has(val) ? cache.get(val) : (cache.set(val, fn(val)) && cache.get(val)); cached.cache = cache; return cached; }; From e3ed2c24b90cd6a55fe0ebfd624c9623ea75822e Mon Sep 17 00:00:00 2001 From: atomiks Date: Wed, 3 Jan 2018 05:10:16 +1100 Subject: [PATCH 4/6] Update memoize.md --- snippets/memoize.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/snippets/memoize.md b/snippets/memoize.md index 7df16e542..856375a69 100644 --- a/snippets/memoize.md +++ b/snippets/memoize.md @@ -3,13 +3,17 @@ 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. +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 = new Map(); - const cached = val => cache.has(val) ? cache.get(val) : (cache.set(val, fn(val)) && cache.get(val)); + 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; }; From af11e80a0abc51dae724ad7f935f9ca4ba0949dd Mon Sep 17 00:00:00 2001 From: David Wu Date: Wed, 3 Jan 2018 12:52:10 +0100 Subject: [PATCH 5/6] Update tag_database --- tag_database | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tag_database b/tag_database index 03d2e17ba..f47f29880 100644 --- a/tag_database +++ b/tag_database @@ -95,10 +95,10 @@ lcm:math lowercaseKeys:object mapObject:array mask:string -maxN:uncategorized +maxN:math median:math memoize:function -minN:uncategorized +minN:math negate:logic nthElement:array objectFromPairs:object From 1b7b382173dac72c315db27af0c13c50e007ea8d Mon Sep 17 00:00:00 2001 From: Travis CI Date: Wed, 3 Jan 2018 11:53:00 +0000 Subject: [PATCH 6/6] Travis build: 908 --- README.md | 95 ++++++++++++++++++++++---------- docs/index.html | 26 ++++++--- snippets/geometricProgression.md | 14 +++-- tag_database | 1 + 4 files changed, 92 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 7248e3af0..abd772b85 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,9 @@ * [`isEven`](#iseven) * [`isPrime`](#isprime) * [`lcm`](#lcm) +* [`maxN`](#maxn) * [`median`](#median) +* [`minN`](#minn) * [`percentile`](#percentile) * [`powerset`](#powerset) * [`primes`](#primes) @@ -288,8 +290,7 @@
View contents -* [`maxN`](#maxn) -* [`minN`](#minn) +* [`geometricProgression`](#geometricprogression)
@@ -3050,6 +3051,31 @@ 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. @@ -3078,6 +3104,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. @@ -5098,40 +5148,27 @@ yesNo('Foo', true); // true --- ## _Uncategorized_ -### maxN +### geometricProgression -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). +Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive and the ratio between two terms is `step`. +Returns an error if `step` equals `1`. -Sort's the array's shallow copy in descending order and returns the first n elements +Use `Array.from()`, `Math.log()` and `Math.floor()` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. +Omit the second argument, `start`, to use a default value of `1`. +Omit the third argument, `step`, to use a default value of `2`. -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); +const geometricProgression = (end, start = 1, step = 2) => + Array.from({ length: Math.floor(Math.log(end / start) / Math.log(step)) + 1 }).map( + (v, i) => start * step ** i + ); ``` ```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] +geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256] +geometricProgression(256, 3); //[3, 6, 12, 24, 48, 96, 192] +geometricProgression(256, 1, 4); //[1, 4, 16, 64, 256] +geometricProgression(256, 2, 1); //Gives error ```
[⬆ back to top](#table-of-contents) diff --git a/docs/index.html b/docs/index.html index 8effda04f..7c981ee44 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

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.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

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.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -661,6 +661,10 @@ 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);
@@ -668,6 +672,10 @@ lcm([1, 3, 4], 5); // 60
 };
 
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
@@ -1152,12 +1160,12 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

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]
-

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]
+

Uncategorized

geometricProgression

Initializes an array containing the numbers in the specified range where start and end are inclusive and the ratio between two terms is step. Returns an error if step equals 1.

Use Array.from(), Math.log() and Math.floor() to create an array of the desired length, Array.map() to fill with the desired values in a range. Omit the second argument, start, to use a default value of 1. Omit the third argument, step, to use a default value of 2.

const geometricProgression = (end, start = 1, step = 2) =>
+  Array.from({ length: Math.floor(Math.log(end / start) / Math.log(step)) + 1 }).map(
+    (v, i) => start * step ** i
+  );
+
geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256]
+geometricProgression(256, 3); //[3, 6, 12, 24, 48, 96, 192]
+geometricProgression(256, 1, 4); //[1, 4, 16, 64, 256]
+geometricProgression(256, 2, 1); //Gives error
 

\ No newline at end of file diff --git a/snippets/geometricProgression.md b/snippets/geometricProgression.md index c1651a852..3f6c2719b 100644 --- a/snippets/geometricProgression.md +++ b/snippets/geometricProgression.md @@ -7,14 +7,16 @@ Use `Array.from()`, `Math.log()` and `Math.floor()` to create an array of the de Omit the second argument, `start`, to use a default value of `1`. Omit the third argument, `step`, to use a default value of `2`. -``` js -const geometricProgression = (end, start = 1,step = 2) => - Array.from({ length:Math.floor(Math.log(end/start)/Math.log(step))+1}).map((v, i) => start * (step ** (i)) ) +```js +const geometricProgression = (end, start = 1, step = 2) => + Array.from({ length: Math.floor(Math.log(end / start) / Math.log(step)) + 1 }).map( + (v, i) => start * step ** i + ); ``` ```js geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256] -geometricProgression(256,3); //[3, 6, 12, 24, 48, 96, 192] -geometricProgression(256,1,4); //[1, 4, 16, 64, 256] -geometricProgression(256,2,1); //Gives error +geometricProgression(256, 3); //[3, 6, 12, 24, 48, 96, 192] +geometricProgression(256, 1, 4); //[1, 4, 16, 64, 256] +geometricProgression(256, 2, 1); //Gives error ``` diff --git a/tag_database b/tag_database index f47f29880..7b4e92890 100644 --- a/tag_database +++ b/tag_database @@ -49,6 +49,7 @@ flip:adapter fromCamelCase:string functionName:function gcd:math +geometricProgression:uncategorized getDaysDiffBetweenDates:date getScrollPosition:browser getStyle:browser