From dcedc10fa0cec134d10ae44079e85297046e209d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 9 Dec 2017 17:24:34 +0200 Subject: [PATCH 1/6] Difference between arrays --- README.md | 10 ++++++++++ snippets/difference-between-arrays.md | 8 ++++++++ 2 files changed, 18 insertions(+) create mode 100644 snippets/difference-between-arrays.md diff --git a/README.md b/README.md index 782f34c86..00b205444 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ * [Capitalize first letter](#capitalize-first-letter) * [Count occurences of a value in array](#count-occurences-of-a-value-in-array) * [Current URL](#current-url) +* [Difference between arrays](#difference-between-arrays) * [Distance between two points](#distance-between-two-points) * [Even or odd number](#even-or-odd-number) * [Factorial](#factorial) @@ -86,6 +87,15 @@ Use `window.location.href` to get current URL. var currentUrl = _ => window.location.href; ``` +### Difference between arrays + +Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`. + +```js +var difference = (arr, values) => + arr.filter(v => values.indexOf(v) === -1); +``` + ### Distance between two points Use `Math.pow()` and `Math.sqrt()` to calculate the Euclidean distance between two points. diff --git a/snippets/difference-between-arrays.md b/snippets/difference-between-arrays.md new file mode 100644 index 000000000..5db03e029 --- /dev/null +++ b/snippets/difference-between-arrays.md @@ -0,0 +1,8 @@ +### Difference between arrays + +Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`. + +```js +var difference = (arr, values) => + arr.filter(v => values.indexOf(v) === -1); +``` From 9c9e2114f6d672ea88999cb04db4e740d700ba58 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 10 Dec 2017 15:02:07 +0200 Subject: [PATCH 2/6] Similarity between arrays, object from key-value pairs --- README.md | 22 +++++++++++++++++++++- snippets/difference-between-arrays.md | 2 +- snippets/object-from-key-value-pairs.md | 8 ++++++++ snippets/similarity-between-arrays.md | 8 ++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 snippets/object-from-key-value-pairs.md create mode 100644 snippets/similarity-between-arrays.md diff --git a/README.md b/README.md index 00b205444..daf8d81dd 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,14 @@ * [Initialize array with range](#initialize-array-with-range) * [Initialize array with values](#initialize-array-with-values) * [Measure time taken by function](#measure-time-taken-by-function) +* [Object from key value pairs](#object-from-key-value-pairs) * [Powerset](#powerset) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) * [Redirect to url](#redirect-to-url) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Scroll to top](#scroll-to-top) +* [Similarity between arrays](#similarity-between-arrays) * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical) * [Sum of array of numbers](#sum-of-array-of-numbers) * [Swap values of two variables](#swap-values-of-two-variables) @@ -89,7 +91,7 @@ var currentUrl = _ => window.location.href; ### Difference between arrays -Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`. +Use `filter()` to remove values that are part of `values`, determined using `indexOf()`. ```js var difference = (arr, values) => @@ -190,6 +192,15 @@ var timeTaken = (f,...args) => { } ``` +### Object from key-value pairs + +Use `map()` to create objects for each key-value pair, combine with `Object.assign()`. + +```js +var objectFromPairs = arr => + Object.assign(...arr.map( v => {return {[v[0]] : v[1]};} )); +``` + ### Powerset Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations. @@ -250,6 +261,15 @@ var scrollToTop = _ => { } ``` +### Similarity between arrays + +Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`. + +```js +var difference = (arr, values) => + arr.filter(v => values.indexOf(v) !== -1); +``` + ### Sort characters in string (alphabetical) Split the string using `split('')`, `sort()` utilizing `localeCompare()`, recombine using `join('')`. diff --git a/snippets/difference-between-arrays.md b/snippets/difference-between-arrays.md index 5db03e029..01e6c5883 100644 --- a/snippets/difference-between-arrays.md +++ b/snippets/difference-between-arrays.md @@ -1,6 +1,6 @@ ### Difference between arrays -Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`. +Use `filter()` to remove values that are part of `values`, determined using `indexOf()`. ```js var difference = (arr, values) => diff --git a/snippets/object-from-key-value-pairs.md b/snippets/object-from-key-value-pairs.md new file mode 100644 index 000000000..e241fcdc9 --- /dev/null +++ b/snippets/object-from-key-value-pairs.md @@ -0,0 +1,8 @@ +### Object from key-value pairs + +Use `map()` to create objects for each key-value pair, combine with `Object.assign()`. + +```js +var objectFromPairs = arr => + Object.assign(...arr.map( v => {return {[v[0]] : v[1]};} )); +``` diff --git a/snippets/similarity-between-arrays.md b/snippets/similarity-between-arrays.md new file mode 100644 index 000000000..b4d08de0a --- /dev/null +++ b/snippets/similarity-between-arrays.md @@ -0,0 +1,8 @@ +### Similarity between arrays + +Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`. + +```js +var difference = (arr, values) => + arr.filter(v => values.indexOf(v) !== -1); +``` From 542a08cf2ad78b29b0049cd789f3c6da12dadc7e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 10 Dec 2017 15:21:35 +0200 Subject: [PATCH 3/6] Generic currying --- README.md | 13 +++++++++++++ snippets/curry.md | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100644 snippets/curry.md diff --git a/README.md b/README.md index daf8d81dd..0a64be235 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ * [Capitalize first letter](#capitalize-first-letter) * [Count occurences of a value in array](#count-occurences-of-a-value-in-array) * [Current URL](#current-url) +* [Curry](#curry) * [Difference between arrays](#difference-between-arrays) * [Distance between two points](#distance-between-two-points) * [Even or odd number](#even-or-odd-number) @@ -89,6 +90,18 @@ Use `window.location.href` to get current URL. var currentUrl = _ => window.location.href; ``` +### Curry + +Use recursion. +If the number of provided arguments (`args`) is sufficient, call the passed function `f`. +Otherwise return a curried function `f` that expects the rest of the arguments. + +```js +curry = f => + (...args) => + args.length >= f.length ? f(...args) : (...otherArgs) => curry(f)(...args, ...otherArgs) +``` + ### Difference between arrays Use `filter()` to remove values that are part of `values`, determined using `indexOf()`. diff --git a/snippets/curry.md b/snippets/curry.md new file mode 100644 index 000000000..9a090fe53 --- /dev/null +++ b/snippets/curry.md @@ -0,0 +1,11 @@ +### Curry + +Use recursion. +If the number of provided arguments (`args`) is sufficient, call the passed function `f`. +Otherwise return a curried function `f` that expects the rest of the arguments. + +```js +curry = f => + (...args) => + args.length >= f.length ? f(...args) : (...otherArgs) => curry(f)(...args, ...otherArgs) +``` From 2fa6e7bba8a2b1a3972ed80f165966d2962efd49 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 10 Dec 2017 16:55:32 +0200 Subject: [PATCH 4/6] Escape regexp --- README.md | 11 +++++++++++ snippets/escape-regular-expression.md | 9 +++++++++ 2 files changed, 20 insertions(+) create mode 100644 snippets/escape-regular-expression.md diff --git a/README.md b/README.md index 0a64be235..12b807a8e 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ * [Curry](#curry) * [Difference between arrays](#difference-between-arrays) * [Distance between two points](#distance-between-two-points) +* [Escape regular expression](#escape-regular-expression) * [Even or odd number](#even-or-odd-number) * [Factorial](#factorial) * [Fibonacci array generator](#fibonacci-array-generator) @@ -120,6 +121,16 @@ var distance = x0, y0, x1, y1 => Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1 - y0, 2)) ``` +### Escape regular expression + +Use `replace()` to escape special characters. + +```js +escapeRegExp = s => + s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} +``` + ### Even or odd number Use `Math.abs()` to extend logic to negative numbers, check using the modulo (`%`) operator. diff --git a/snippets/escape-regular-expression.md b/snippets/escape-regular-expression.md new file mode 100644 index 000000000..b653c066d --- /dev/null +++ b/snippets/escape-regular-expression.md @@ -0,0 +1,9 @@ +### Escape regular expression + +Use `replace()` to escape special characters. + +```js +escapeRegExp = s => + s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} +``` From b30a7af75683993587e77d48609e792adf7e8435 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 10 Dec 2017 17:59:07 +0200 Subject: [PATCH 5/6] Housekeeping, head, tail, init and last of list --- README.md | 44 +++++++++++++++++++++++++-- snippets/curry.md | 2 +- snippets/escape-regular-expression.md | 2 +- snippets/head-of-list.md | 8 +++++ snippets/initial-of-list.md | 8 +++++ snippets/last-of-list.md | 8 +++++ snippets/tail-of-list.md | 8 +++++ 7 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 snippets/head-of-list.md create mode 100644 snippets/initial-of-list.md create mode 100644 snippets/last-of-list.md create mode 100644 snippets/tail-of-list.md diff --git a/README.md b/README.md index 12b807a8e..14410e196 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,11 @@ * [Fibonacci array generator](#fibonacci-array-generator) * [Flatten array](#flatten-array) * [Greatest common divisor (GCD)](#greatest-common-divisor-gcd) +* [Head of list](#head-of-list) +* [Initial of list](#initial-of-list) * [Initialize array with range](#initialize-array-with-range) * [Initialize array with values](#initialize-array-with-values) +* [Last of list](#last-of-list) * [Measure time taken by function](#measure-time-taken-by-function) * [Object from key value pairs](#object-from-key-value-pairs) * [Powerset](#powerset) @@ -36,6 +39,7 @@ * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical) * [Sum of array of numbers](#sum-of-array-of-numbers) * [Swap values of two variables](#swap-values-of-two-variables) +* [Tail of list](#tail-of-list) * [Unique values of array](#unique-values-of-array) * [URL parameters](#url-parameters) * [UUID generator](#uuid-generator) @@ -98,7 +102,7 @@ If the number of provided arguments (`args`) is sufficient, call the passed func Otherwise return a curried function `f` that expects the rest of the arguments. ```js -curry = f => +var curry = f => (...args) => args.length >= f.length ? f(...args) : (...otherArgs) => curry(f)(...args, ...otherArgs) ``` @@ -126,7 +130,7 @@ var distance = x0, y0, x1, y1 => Use `replace()` to escape special characters. ```js -escapeRegExp = s => +var escapeRegExp = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } ``` @@ -183,6 +187,24 @@ Otherwise, return the GCD of `y` and the remainder of the division `x/y`. var gcd = (x , y) => !y ? x : gcd(y, x % y); ``` +### Head of list + +Return `arr[0]`. + +```js +var head = arr => arr[0]; +} +``` + +### Initial of list + +Return `arr.slice(0,-1)`. + +```js +var initial = arr => arr.slice(0,-1); +} +``` + ### Initialize array with range Use `Array(end-start)` to create an array of the desired length, `map()` to fill with the desired values in a range. @@ -203,6 +225,15 @@ var initializeArray = (n, v = 0) => Array(n).fill(v); ``` +### Last of list + +Return `arr.slice(-1)[0]`. + +```js +var initial = arr => arr.slice(-1)[0]; +} +``` + ### Measure time taken by function Use `performance.now()` to get start and end time for the function, `console.log()` the time taken. @@ -320,6 +351,15 @@ Use array destructuring to swap values between two variables. [varA, varB] = [varB, varA]; ``` +### Tail of list + +Return `arr.slice(1)`. + +```js +var tail = arr => arr.slice(1); +} +``` + ### Unique values of array Use `reduce()` to accumulate all unique values in an array. diff --git a/snippets/curry.md b/snippets/curry.md index 9a090fe53..a30e74433 100644 --- a/snippets/curry.md +++ b/snippets/curry.md @@ -5,7 +5,7 @@ If the number of provided arguments (`args`) is sufficient, call the passed func Otherwise return a curried function `f` that expects the rest of the arguments. ```js -curry = f => +var curry = f => (...args) => args.length >= f.length ? f(...args) : (...otherArgs) => curry(f)(...args, ...otherArgs) ``` diff --git a/snippets/escape-regular-expression.md b/snippets/escape-regular-expression.md index b653c066d..00367ec3d 100644 --- a/snippets/escape-regular-expression.md +++ b/snippets/escape-regular-expression.md @@ -3,7 +3,7 @@ Use `replace()` to escape special characters. ```js -escapeRegExp = s => +var escapeRegExp = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } ``` diff --git a/snippets/head-of-list.md b/snippets/head-of-list.md new file mode 100644 index 000000000..0fec83c44 --- /dev/null +++ b/snippets/head-of-list.md @@ -0,0 +1,8 @@ +### Head of list + +Return `arr[0]`. + +```js +var head = arr => arr[0]; +} +``` diff --git a/snippets/initial-of-list.md b/snippets/initial-of-list.md new file mode 100644 index 000000000..a5afb4f04 --- /dev/null +++ b/snippets/initial-of-list.md @@ -0,0 +1,8 @@ +### Initial of list + +Return `arr.slice(0,-1)`. + +```js +var initial = arr => arr.slice(0,-1); +} +``` diff --git a/snippets/last-of-list.md b/snippets/last-of-list.md new file mode 100644 index 000000000..2bbf2fe12 --- /dev/null +++ b/snippets/last-of-list.md @@ -0,0 +1,8 @@ +### Last of list + +Return `arr.slice(-1)[0]`. + +```js +var initial = arr => arr.slice(-1)[0]; +} +``` diff --git a/snippets/tail-of-list.md b/snippets/tail-of-list.md new file mode 100644 index 000000000..5c52c2ac4 --- /dev/null +++ b/snippets/tail-of-list.md @@ -0,0 +1,8 @@ +### Tail of list + +Return `arr.slice(1)`. + +```js +var tail = arr => arr.slice(1); +} +``` From 4f299f070564145ed6463b3d74a7fcb121732fb8 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 10 Dec 2017 18:00:03 +0200 Subject: [PATCH 6/6] Housekeeping --- README.md | 4 ---- snippets/head-of-list.md | 1 - snippets/initial-of-list.md | 1 - snippets/last-of-list.md | 1 - snippets/tail-of-list.md | 1 - 5 files changed, 8 deletions(-) diff --git a/README.md b/README.md index 14410e196..c1d9f844e 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,6 @@ Return `arr[0]`. ```js var head = arr => arr[0]; -} ``` ### Initial of list @@ -202,7 +201,6 @@ Return `arr.slice(0,-1)`. ```js var initial = arr => arr.slice(0,-1); -} ``` ### Initialize array with range @@ -231,7 +229,6 @@ Return `arr.slice(-1)[0]`. ```js var initial = arr => arr.slice(-1)[0]; -} ``` ### Measure time taken by function @@ -357,7 +354,6 @@ Return `arr.slice(1)`. ```js var tail = arr => arr.slice(1); -} ``` ### Unique values of array diff --git a/snippets/head-of-list.md b/snippets/head-of-list.md index 0fec83c44..b0b8e6305 100644 --- a/snippets/head-of-list.md +++ b/snippets/head-of-list.md @@ -4,5 +4,4 @@ Return `arr[0]`. ```js var head = arr => arr[0]; -} ``` diff --git a/snippets/initial-of-list.md b/snippets/initial-of-list.md index a5afb4f04..8ed934543 100644 --- a/snippets/initial-of-list.md +++ b/snippets/initial-of-list.md @@ -4,5 +4,4 @@ Return `arr.slice(0,-1)`. ```js var initial = arr => arr.slice(0,-1); -} ``` diff --git a/snippets/last-of-list.md b/snippets/last-of-list.md index 2bbf2fe12..4397d5980 100644 --- a/snippets/last-of-list.md +++ b/snippets/last-of-list.md @@ -4,5 +4,4 @@ Return `arr.slice(-1)[0]`. ```js var initial = arr => arr.slice(-1)[0]; -} ``` diff --git a/snippets/tail-of-list.md b/snippets/tail-of-list.md index 5c52c2ac4..1b5adfd83 100644 --- a/snippets/tail-of-list.md +++ b/snippets/tail-of-list.md @@ -4,5 +4,4 @@ Return `arr.slice(1)`. ```js var tail = arr => arr.slice(1); -} ```