Adapter
ary
Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).
const ary = (fn, n) => (...args) => fn(...args.slice(0, n)); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
ary
Creates a function that accepts up to
narguments, ignoring any additional arguments.Call the provided function,
fn, with up tonarguments, usingArray.slice(0,n)and the spread operator (...).const ary = (fn, n) => (...args) => fn(...args.slice(0, n));const firstTwoMax = ary(Math.max, 2); [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]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); @@ -448,6 +448,13 @@ Object.assig return index === -1 ? 0 : arr.length - index; };sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1 +stableSortadvanced
Performs stable sorting of an array, preserving the initial indexes of items when their values are the same. Does not mutate the original array, but returns a new array instead.
Use
Array.map()to pair each element of the input array with its corresponding index. UseArray.sort()and acomparefunction to sort the list, preserving their initial order if the items compared are equal. UseArray.map()to convert back to the initial array items.const stableSort = (arr, compare) => + arr + .map((item, index) => ({ item, index })) + .sort((a, b) => compare(a.item, b.item) || a.index - b.index) + .map(({ item }) => item); +const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +const stable = stableSort(arr, () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]symmetricDifference
Returns the symmetric difference between two arrays.
Create a
Setfrom each array, then useArray.filter()on each of them to only keep values not contained in the other.const symmetricDifference = (a, b) => { const sA = new Set(a), sB = new Set(b); diff --git a/snippets/stableSort.md b/snippets/stableSort.md new file mode 100644 index 000000000..5a37fb1d6 --- /dev/null +++ b/snippets/stableSort.md @@ -0,0 +1,21 @@ +### stableSort + +Performs stable sorting of an array, preserving the initial indexes of items when their values are the same. +Does not mutate the original array, but returns a new array instead. + +Use `Array.map()` to pair each element of the input array with its corresponding index. +Use `Array.sort()` and a `compare` function to sort the list, preserving their initial order if the items compared are equal. +Use `Array.map()` to convert back to the initial array items. + +```js +const stableSort = (arr, compare) => + arr + .map((item, index) => ({ item, index })) + .sort((a, b) => compare(a.item, b.item) || a.index - b.index) + .map(({ item }) => item); +``` + +```js +const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +const stable = stableSort(arr, () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +``` diff --git a/tag_database b/tag_database index d27320a9d..4783045a3 100644 --- a/tag_database +++ b/tag_database @@ -234,6 +234,7 @@ sortedLastIndex:array,math sortedLastIndexBy:array,math,function splitLines:string spreadOver:adapter +stableSort:array,sort,advanced standardDeviation:math,array stripHTMLTags:string,utility,regexp sum:math,array diff --git a/test/stableSort/stableSort.js b/test/stableSort/stableSort.js new file mode 100644 index 000000000..452d6b726 --- /dev/null +++ b/test/stableSort/stableSort.js @@ -0,0 +1,6 @@ +const stableSort = (arr, compare) => +arr +.map((item, index) => ({ item, index })) +.sort((a, b) => compare(a.item, b.item) || a.index - b.index) +.map(({ item }) => item); +module.exports = stableSort; \ No newline at end of file diff --git a/test/stableSort/stableSort.test.js b/test/stableSort/stableSort.test.js new file mode 100644 index 000000000..e8cf95496 --- /dev/null +++ b/test/stableSort/stableSort.test.js @@ -0,0 +1,17 @@ +const test = require('tape'); +const stableSort = require('./stableSort.js'); + +test('Testing stableSort', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof stableSort === 'function', 'stableSort is a Function'); + //t.deepEqual(stableSort(args..), 'Expected'); + //t.equal(stableSort(args..), 'Expected'); + //t.false(stableSort(args..), 'Expected'); + //t.throws(stableSort(args..), 'Expected'); + + const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + const compare = () => 0; + t.deepEqual(stableSort(arr, compare), arr, 'Array is properly sorted'); + t.end(); +}); diff --git a/test/testlog b/test/testlog index ac66eda96..a8e0e2f68 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Sun Feb 18 2018 19:56:04 GMT+0530 (India Standard Time) +Test log for: Sun Feb 18 2018 20:24:43 GMT+0000 (UTC) > 30-seconds-of-code@0.0.2 test R:\github\30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -1527,6 +1527,11 @@ Test log for: Sun Feb 18 2018 19:56:04 GMT+0530 (India Standard Time) √ spreadOver is a Function √ Takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function. + Testing stableSort + + ✔ stableSort is a Function + ✔ Array is properly sorted + Testing standardDeviation √ standardDeviation is a Function @@ -1915,6 +1920,7 @@ Test log for: Sun Feb 18 2018 19:56:04 GMT+0530 (India Standard Time) √ Works with multiple promises + total: 974 passing: 974 duration: 2.9s