From 1fd8ea262e907ca6dc3d68854fe7479bcd6c0e3d Mon Sep 17 00:00:00 2001 From: Travis CI Date: Sun, 31 Dec 2017 12:13:21 +0000 Subject: [PATCH] Travis build: 710 [ci skip] --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ docs/index.html | 12 +++++++++++- snippets/sampleSize.md | 10 +++++----- tag_database | 1 + 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2bcb93460..f185c714d 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,15 @@ +### _Uncategorized_ + +
+View contents + +* [`sampleSize`](#samplesize) + +
+ --- ## 🔌 Adapter @@ -4361,6 +4370,35 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _Uncategorized_ + +### sampleSize + +Gets `n` random elements at unique keys from `array` up to the size of `array`. + +Shuffle the array using the [Fisher-Yates algorithm](https://github.com/chalarangelo/30-seconds-of-code#shuffle). +Use `Array.slice()` to get the first `n` elements. +Omit the second argument, `n` to get only one element at random from the array. + +```js +const sampleSize = ([...arr], n = 1) => { + let m = arr.length; + while (m) { + const i = Math.floor(Math.random() * m--); + [arr[m], arr[i]] = [arr[i], arr[m]]; + } + return arr.slice(0, n); +}; +``` + +```js +sampleSize([1, 2, 3], 2); // [3,1] +sampleSize([1, 2, 3], 4); // [2,3,1] +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index d10bb750a..6a66cb59d 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 ]
@@ -905,4 +905,14 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
+

Uncategorized

sampleSize

Gets n random elements at unique keys from array up to the size of array.

Shuffle the array using the Fisher-Yates algorithm. Use Array.slice() to get the first n elements. Omit the second argument, n to get only one element at random from the array.

const sampleSize = ([...arr], n = 1) => {
+  let m = arr.length;
+  while (m) {
+    const i = Math.floor(Math.random() * m--);
+    [arr[m], arr[i]] = [arr[i], arr[m]];
+  }
+  return arr.slice(0, n);
+};
+
sampleSize([1, 2, 3], 2); // [3,1]
+sampleSize([1, 2, 3], 4); // [2,3,1]
 

\ No newline at end of file diff --git a/snippets/sampleSize.md b/snippets/sampleSize.md index f6187bdea..7b2480cba 100644 --- a/snippets/sampleSize.md +++ b/snippets/sampleSize.md @@ -7,17 +7,17 @@ Use `Array.slice()` to get the first `n` elements. Omit the second argument, `n` to get only one element at random from the array. ```js -const sampleSize = ([...arr],n=1) => { +const sampleSize = ([...arr], n = 1) => { let m = arr.length; while (m) { const i = Math.floor(Math.random() * m--); [arr[m], arr[i]] = [arr[i], arr[m]]; } - return arr.slice(0,n) -} + return arr.slice(0, n); +}; ``` ```js -sampleSize([1,2,3],2); // [3,1] -sampleSize([1,2,3],4); // [2,3,1] +sampleSize([1, 2, 3], 2); // [3,1] +sampleSize([1, 2, 3], 4); // [2,3,1] ``` diff --git a/tag_database b/tag_database index db90b4d02..b06cb8280 100644 --- a/tag_database +++ b/tag_database @@ -114,6 +114,7 @@ RGBToHex:utility round:math runPromisesInSeries:function sample:array +sampleSize:uncategorized scrollToTop:browser sdbm:utility select:object