From 836ae3905b65ca6d7aa9be93ffef716e80172eaf Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 24 Jan 2018 10:54:56 +0000 Subject: [PATCH] Travis build: 1374 --- README.md | 49 ++++++++++++++++++++++++++++++++++++ docs/index.html | 9 ++++++- snippets/intersectionWith.md | 3 +-- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9e5bd5721..078ad19d4 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,8 @@ average(1, 2, 3); * [`initializeArrayWithRangeRight`](#initializearraywithrangeright) * [`initializeArrayWithValues`](#initializearraywithvalues) * [`intersection`](#intersection) +* [`intersectionBy`](#intersectionby) +* [`intersectionWith`](#intersectionwith) * [`isSorted`](#issorted) * [`join`](#join) * [`last`](#last) @@ -1179,6 +1181,53 @@ intersection([1, 2, 3], [4, 3, 2]); // [2,3]
[⬆ Back to top](#table-of-contents) +### intersectionBy + +Returns a list of elements that exist in both arrays, after applying the provided function to each array element of both. + +Create a `Set` by applying `fn` to all elements in `b`, then use `Array.filter()` on `a` to only keep elements, which produce values contained in `b` when `fn` is applied to them. + +```js +const intersectionBy = (a, b, fn) => { + const s = new Set(b.map(x => fn(x))); + return a.filter(x => s.has(fn(x))); +}; +``` + +
+Examples + +```js +intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [2.1] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### intersectionWith + +Returns a list of elements that exist in both arrays, using a provided comparator function. + +Use `Array.filter()` and `Array.findIndex()` in combination with the provided comparator to determine intersecting values. + +```js +const intersectionWith = (a, b, comp) => a.filter(x => b.findIndex(y => comp(x, y)) !== -1); +``` + +
+Examples + +```js +intersectionWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0, 3.9], (a, b) => Math.round(a) === Math.round(b)); // [1.5, 3, 0] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### isSorted Returns `1` if the array is sorted in ascending order, `-1` if it is sorted in descending order or `0` if it is not sorted. diff --git a/docs/index.html b/docs/index.html index 8180284a3..e44691aac 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 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);
+      }

logo 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 ]
@@ -187,6 +187,13 @@ Object.assig
   return a.filter(x => s.has(x));
 };
 
intersection([1, 2, 3], [4, 3, 2]); // [2,3]
+

intersectionBy

Returns a list of elements that exist in both arrays, after applying the provided function to each array element of both.

Create a Set by applying fn to all elements in b, then use Array.filter() on a to only keep elements, which produce values contained in b when fn is applied to them.

const intersectionBy = (a, b, fn) => {
+  const s = new Set(b.map(x => fn(x)));
+  return a.filter(x => s.has(fn(x)));
+};
+
intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [2.1]
+

intersectionWith

Returns a list of elements that exist in both arrays, using a provided comparator function.

Use Array.filter() and Array.findIndex() in combination with the provided comparator to determine intersecting values.

const intersectionWith = (a, b, comp) => a.filter(x => b.findIndex(y => comp(x, y)) !== -1);
+
intersectionWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0, 3.9], (a, b) => Math.round(a) === Math.round(b)); // [1.5, 3, 0]
 

isSorted

Returns 1 if the array is sorted in ascending order, -1 if it is sorted in descending order or 0 if it is not sorted.

Calculate the ordering direction for the first two elements. Use Object.entries() to loop over array objects and compare them in pairs. Return 0 if the direction changes or the direction if the last element is reached.

const isSorted = arr => {
   const direction = arr[0] > arr[1] ? -1 : 1;
   for (let [i, val] of arr.entries())
diff --git a/snippets/intersectionWith.md b/snippets/intersectionWith.md
index ae5483c47..4aa7e0874 100644
--- a/snippets/intersectionWith.md
+++ b/snippets/intersectionWith.md
@@ -5,8 +5,7 @@ Returns a list of elements that exist in both arrays, using a provided comparato
 Use `Array.filter()` and `Array.findIndex()` in combination with the provided comparator to determine intersecting values.
 
 ```js
-const intersectionWith = (a, b, comp) =>
-  a.filter(x => b.findIndex(y => comp(x, y)) !== -1);
+const intersectionWith = (a, b, comp) => a.filter(x => b.findIndex(y => comp(x, y)) !== -1);
 ```
 
 ```js