From b48e971c5063fd4dc79a545a2e94344e35b3b019 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Thu, 14 Dec 2017 18:49:54 +0530 Subject: [PATCH 1/5] Create sqaure_it.md I don't exactly know the use of this snippet but contributors can recommend on this approach of `.reduce()`. --- snippets/sqaure_it.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/sqaure_it.md diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md new file mode 100644 index 000000000..9f25d895e --- /dev/null +++ b/snippets/sqaure_it.md @@ -0,0 +1,18 @@ +### Square The Data + +Pass an array of integers you want to sqaure it +Here using `.reduce()` the new object is accumulator and one by one values from array are passed and go through function. + +``` +const arr = [1,2,3,4,5,6,7]; +arr.reduce(function(a,b){ +a[b] = b * b; +return a; +},{}) // {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} +// +callback accumulator currentValue currentIndex array return value +first call 1 1 0 [0, 1, 2, 3, 4] 1 +second call 2 2 1 [0, 1, 2, 3, 4] 4 +third call 3 3 2 [0, 1, 2, 3, 4] 9 +... +``` From e8abd35702fcedee5aaa790f5012672a5d429a71 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sun, 17 Dec 2017 22:13:10 +0530 Subject: [PATCH 2/5] Update sqaure_it.md --- snippets/sqaure_it.md | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md index 9f25d895e..ff2a274cd 100644 --- a/snippets/sqaure_it.md +++ b/snippets/sqaure_it.md @@ -1,18 +1,8 @@ ### Square The Data -Pass an array of integers you want to sqaure it -Here using `.reduce()` the new object is accumulator and one by one values from array are passed and go through function. +Use mapObject to return a object from array passed as an argument which gets sqaured as math operation -``` -const arr = [1,2,3,4,5,6,7]; -arr.reduce(function(a,b){ -a[b] = b * b; -return a; -},{}) // {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49} -// -callback accumulator currentValue currentIndex array return value -first call 1 1 0 [0, 1, 2, 3, 4] 1 -second call 2 2 1 [0, 1, 2, 3, 4] 4 -third call 3 3 2 [0, 1, 2, 3, 4] 9 -... +```js +const squareIt = arr => mapObject(arr, a => a*a) +squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 } ``` From ad9b16b3f6d837250486f7ceb67997eee61a582e Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sun, 17 Dec 2017 22:15:36 +0530 Subject: [PATCH 3/5] Update sqaure_it.md --- snippets/sqaure_it.md | 1 + 1 file changed, 1 insertion(+) diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md index ff2a274cd..665932e3d 100644 --- a/snippets/sqaure_it.md +++ b/snippets/sqaure_it.md @@ -3,6 +3,7 @@ Use mapObject to return a object from array passed as an argument which gets sqaured as math operation ```js +const mapObject = (arr, fn) => (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( ) const squareIt = arr => mapObject(arr, a => a*a) squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 } ``` From c3de880e419da8acbbc3d83cd71b607a7b731361 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Mon, 18 Dec 2017 08:33:35 +0530 Subject: [PATCH 4/5] Update sqaure_it.md --- snippets/sqaure_it.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md index 665932e3d..eed3eb137 100644 --- a/snippets/sqaure_it.md +++ b/snippets/sqaure_it.md @@ -1,6 +1,8 @@ ### Square The Data -Use mapObject to return a object from array passed as an argument which gets sqaured as math operation +Using an anonymous inner function scope we declare an undefined memory space using closures for storing a return value. We then use a new Array to store the array with a map of the function over it's data set and a comma operator to return a second step without needing to move from one context to another thank to closures and order of operations. + +Notice the need to declare an order of operations(parenthesis) around the anonymous inner function so we can call it immediately. ```js const mapObject = (arr, fn) => (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( ) From 42bff6deb60f13e91c140e31ede53c2f89767aa3 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 18 Dec 2017 12:11:58 +0200 Subject: [PATCH 5/5] Update and rename sqaure_it.md to mapObject.md --- snippets/mapObject.md | 14 ++++++++++++++ snippets/sqaure_it.md | 11 ----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 snippets/mapObject.md delete mode 100644 snippets/sqaure_it.md diff --git a/snippets/mapObject.md b/snippets/mapObject.md new file mode 100644 index 000000000..102795a73 --- /dev/null +++ b/snippets/mapObject.md @@ -0,0 +1,14 @@ +### mapObject + +Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value. + +Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new `Array` to stor the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations). + +```js +const mapObject = (arr, fn) => + (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( ); +/* +const squareIt = arr => mapObject(arr, a => a*a) +squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 } +*/ +``` diff --git a/snippets/sqaure_it.md b/snippets/sqaure_it.md deleted file mode 100644 index eed3eb137..000000000 --- a/snippets/sqaure_it.md +++ /dev/null @@ -1,11 +0,0 @@ -### Square The Data - -Using an anonymous inner function scope we declare an undefined memory space using closures for storing a return value. We then use a new Array to store the array with a map of the function over it's data set and a comma operator to return a second step without needing to move from one context to another thank to closures and order of operations. - -Notice the need to declare an order of operations(parenthesis) around the anonymous inner function so we can call it immediately. - -```js -const mapObject = (arr, fn) => (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( ) -const squareIt = arr => mapObject(arr, a => a*a) -squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 } -```