From f7d2695866a5194856ef791acb0fd98ab8aae0bb Mon Sep 17 00:00:00 2001 From: conblem Date: Tue, 12 Dec 2017 17:01:01 +0100 Subject: [PATCH 1/5] Pipe example --- snippets/pipe.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 snippets/pipe.md diff --git a/snippets/pipe.md b/snippets/pipe.md new file mode 100644 index 000000000..16f3e59d6 --- /dev/null +++ b/snippets/pipe.md @@ -0,0 +1,8 @@ +### Pipe + +Use `reduce()` to pass value through functions. + +```js +const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg); +// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA==" +``` From eba53699898da07e029e6e87596a798643dd4bfb Mon Sep 17 00:00:00 2001 From: conblem Date: Tue, 12 Dec 2017 17:03:14 +0100 Subject: [PATCH 2/5] Use full name for reduce --- README.md | 10 ++++++++++ snippets/pipe.md | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c8aa9c5a..4233f6171 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ * [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) +* [Pipe](#pipe) * [Powerset](#powerset) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) @@ -327,6 +328,15 @@ const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {}); // objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} ``` +### Pipe + +Use `Array.reduce()` to pass value through functions. + +```js +const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg); +// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA==" +``` + ### Powerset Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations. diff --git a/snippets/pipe.md b/snippets/pipe.md index 16f3e59d6..c61ec042c 100644 --- a/snippets/pipe.md +++ b/snippets/pipe.md @@ -1,6 +1,6 @@ ### Pipe -Use `reduce()` to pass value through functions. +Use `Array.reduce()` to pass value through functions. ```js const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg); From 542a4f94b1a32049121692f6244208776fe8f2f0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 12 Dec 2017 18:12:24 +0200 Subject: [PATCH 3/5] Deep flatten and housekeeping Changed original flatten to be named deepFlatten, added normal flatten, improved some other snippets. --- README.md | 25 +++++++++++++------ .../count-occurrences-of-a-value-in-array.md | 2 +- snippets/deep-flatten-array.md | 10 ++++++++ snippets/escape-regular-expression.md | 3 +-- snippets/flatten-array.md | 8 +++--- 5 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 snippets/deep-flatten-array.md diff --git a/README.md b/README.md index cad3dbcba..3480f7f8e 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ * [Count occurrences of a value in array](#count-occurrences-of-a-value-in-array) * [Current URL](#current-url) * [Curry](#curry) +* [Deep flatten array](#deep-flatten-array) * [Difference between arrays](#difference-between-arrays) * [Distance between two points](#distance-between-two-points) * [Divisible by number](#divisible-by-number) @@ -115,7 +116,7 @@ const palindrome = str => Use `reduce()` to increment a counter each time you encounter the specific value inside the array. ```js -const countOccurrences = (arr, value) => arr.reduce((a, v) => v===value ? a + 1 : a + 0, 0); +const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0); // countOccurrences([1,1,2,1,2,3], 1) -> 3 ``` @@ -141,6 +142,17 @@ const curry = f => // curry(Math.pow)(2)(10) -> 1024 ``` +### Deep flatten array + +Use recursion. +Use `reduce()` to get all elements that are not arrays, flatten each element that is an array. + +```js +const deepFlatten = arr => + arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []); +// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5] +``` + ### Difference between arrays Use `filter()` to remove values that are part of `values`, determined using `includes()`. @@ -173,8 +185,7 @@ const isDivisible = (dividend, divisor) => dividend % divisor === 0; Use `replace()` to escape special characters. ```js -const escapeRegExp = s => - s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // escapeRegExp('(test)') -> \\(test\\) ``` @@ -225,13 +236,11 @@ const unique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i)); ### Flatten array -Use recursion. -Use `reduce()` to get all elements that are not arrays, flatten each element that is an array. +Use `reduce()` to get all elements inside the array and `concat()` to flatten them. ```js -const flatten = arr => - arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []); -// flatten([1,[2],[[3],4],5]) -> [1,2,3,4,5] +const flatten = arr => arr.reduce( (a, v) => a.concat(v), []); +// flatten([1,[2],3,4) -> [1,2,3,4] ``` ## Get scroll position diff --git a/snippets/count-occurrences-of-a-value-in-array.md b/snippets/count-occurrences-of-a-value-in-array.md index 552ad9f39..89e3b5b6d 100644 --- a/snippets/count-occurrences-of-a-value-in-array.md +++ b/snippets/count-occurrences-of-a-value-in-array.md @@ -3,6 +3,6 @@ Use `reduce()` to increment a counter each time you encounter the specific value inside the array. ```js -const countOccurrences = (arr, value) => arr.reduce((a, v) => v===value ? a + 1 : a + 0, 0); +const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0); // countOccurrences([1,1,2,1,2,3], 1) -> 3 ``` diff --git a/snippets/deep-flatten-array.md b/snippets/deep-flatten-array.md new file mode 100644 index 000000000..545c4c8d4 --- /dev/null +++ b/snippets/deep-flatten-array.md @@ -0,0 +1,10 @@ +### Deep flatten array + +Use recursion. +Use `reduce()` to get all elements that are not arrays, flatten each element that is an array. + +```js +const deepFlatten = arr => + arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []); +// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5] +``` diff --git a/snippets/escape-regular-expression.md b/snippets/escape-regular-expression.md index 4bd77a0d9..fb204a451 100644 --- a/snippets/escape-regular-expression.md +++ b/snippets/escape-regular-expression.md @@ -3,7 +3,6 @@ Use `replace()` to escape special characters. ```js -const escapeRegExp = s => - s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // escapeRegExp('(test)') -> \\(test\\) ``` diff --git a/snippets/flatten-array.md b/snippets/flatten-array.md index f3b1c9f4b..c4afd4847 100644 --- a/snippets/flatten-array.md +++ b/snippets/flatten-array.md @@ -1,10 +1,8 @@ ### Flatten array -Use recursion. -Use `reduce()` to get all elements that are not arrays, flatten each element that is an array. +Use `reduce()` to get all elements inside the array and `concat()` to flatten them. ```js -const flatten = arr => - arr.reduce( (a, v) => a.concat( Array.isArray(v) ? flatten(v) : v ), []); -// flatten([1,[2],[[3],4],5]) -> [1,2,3,4,5] +const flatten = arr => arr.reduce( (a, v) => a.concat(v), []); +// flatten([1,[2],3,4) -> [1,2,3,4] ``` From d1da6a65642ed9d33e31c851ad2ce947ee51558c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 12 Dec 2017 18:12:59 +0200 Subject: [PATCH 4/5] Scroll position title size --- README.md | 2 +- snippets/get-scroll-position.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3480f7f8e..cb5d6c3df 100644 --- a/README.md +++ b/README.md @@ -243,7 +243,7 @@ const flatten = arr => arr.reduce( (a, v) => a.concat(v), []); // flatten([1,[2],3,4) -> [1,2,3,4] ``` -## Get scroll position +### Get scroll position Use `pageXOffset` and `pageYOffset` if they are defined, otherwise `scrollLeft` and `scrollTop`. You can omit `el` to use a default value of `window`. diff --git a/snippets/get-scroll-position.md b/snippets/get-scroll-position.md index ea823fd18..7e367168f 100644 --- a/snippets/get-scroll-position.md +++ b/snippets/get-scroll-position.md @@ -1,4 +1,4 @@ -## Get scroll position +### Get scroll position Use `pageXOffset` and `pageYOffset` if they are defined, otherwise `scrollLeft` and `scrollTop`. You can omit `el` to use a default value of `window`. From e2d2b6aa417b0c1c3c5b78fbdeeafa3e6bae1c8a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 12 Dec 2017 18:21:53 +0200 Subject: [PATCH 5/5] Updated snippets Mainly for better readability --- README.md | 13 ++++++------- snippets/initialize-array-with-values.md | 4 ++-- snippets/measure-time-taken-by-function.md | 4 ++-- snippets/object-from-key-value-pairs.md | 2 +- snippets/sum-of-array-of-numbers.md | 3 +-- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5713f6300..701b108ef 100644 --- a/README.md +++ b/README.md @@ -299,10 +299,10 @@ const initializeArrayRange = (end, start = 0) => ### Initialize array with values Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values. -You can omit `v` to use a default value of `0`. +You can omit `value` to use a default value of `0`. ```js -const initializeArray = (n, v = 0) => Array(n).fill(v); +const initializeArray = (n, value = 0) => Array(n).fill(value); // initializeArray(5, 2) -> [2,2,2,2,2] ``` @@ -321,8 +321,8 @@ Use `performance.now()` to get start and end time for the function, `console.log First argument is the function name, subsequent arguments are passed to the function. ```js -const timeTaken = (f,...args) => { - var t0 = performance.now(), r = f(...args); +const timeTaken = (func,...args) => { + var t0 = performance.now(), r = func(...args); console.log(performance.now() - t0); return r; } @@ -334,7 +334,7 @@ const timeTaken = (f,...args) => { Use `Array.reduce()` to create and combine key-value pairs. ```js -const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {}); +const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {}); // objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} ``` @@ -447,8 +447,7 @@ const sortCharactersInString = str => Use `reduce()` to add each value to an accumulator, initialized with a value of `0`. ```js -const sum = arr => - arr.reduce( (acc , val) => acc + val, 0); +const sum = arr => arr.reduce( (acc , val) => acc + val, 0); // sum([1,2,3,4]) -> 10 ``` diff --git a/snippets/initialize-array-with-values.md b/snippets/initialize-array-with-values.md index 3ee75f97c..d8a18110f 100644 --- a/snippets/initialize-array-with-values.md +++ b/snippets/initialize-array-with-values.md @@ -1,9 +1,9 @@ ### Initialize array with values Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values. -You can omit `v` to use a default value of `0`. +You can omit `value` to use a default value of `0`. ```js -const initializeArray = (n, v = 0) => Array(n).fill(v); +const initializeArray = (n, value = 0) => Array(n).fill(value); // initializeArray(5, 2) -> [2,2,2,2,2] ``` diff --git a/snippets/measure-time-taken-by-function.md b/snippets/measure-time-taken-by-function.md index 5f39c34ed..8586f59d1 100644 --- a/snippets/measure-time-taken-by-function.md +++ b/snippets/measure-time-taken-by-function.md @@ -4,8 +4,8 @@ Use `performance.now()` to get start and end time for the function, `console.log First argument is the function name, subsequent arguments are passed to the function. ```js -const timeTaken = (f,...args) => { - var t0 = performance.now(), r = f(...args); +const timeTaken = (func,...args) => { + var t0 = performance.now(), r = func(...args); console.log(performance.now() - t0); return r; } diff --git a/snippets/object-from-key-value-pairs.md b/snippets/object-from-key-value-pairs.md index 0f7a60e44..df01da0ba 100644 --- a/snippets/object-from-key-value-pairs.md +++ b/snippets/object-from-key-value-pairs.md @@ -3,6 +3,6 @@ Use `Array.reduce()` to create and combine key-value pairs. ```js -const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {}); +const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {}); // objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} ``` diff --git a/snippets/sum-of-array-of-numbers.md b/snippets/sum-of-array-of-numbers.md index f2805b08f..fcf01949c 100644 --- a/snippets/sum-of-array-of-numbers.md +++ b/snippets/sum-of-array-of-numbers.md @@ -3,7 +3,6 @@ Use `reduce()` to add each value to an accumulator, initialized with a value of `0`. ```js -const sum = arr => - arr.reduce( (acc , val) => acc + val, 0); +const sum = arr => arr.reduce( (acc , val) => acc + val, 0); // sum([1,2,3,4]) -> 10 ```