Created
March 11, 2019 04:30
-
-
Save pertrai1/2c397978f374987f4970b1c6b8986599 to your computer and use it in GitHub Desktop.
Map Reduce Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const map = (fn, arr) => arr.reduce((acc, item) => { | |
return acc.concat(fn(item)); | |
}, []); | |
const myArray = [2, 4, 6, 8]; | |
const reduceFn = item => item * 2; | |
const myReducer = map(reduceFn, myArray); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fn, arr) => arr.reduce((acc, item) => { | |
return acc.concat(fn(item)); | |
}, []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
item => item * 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reduceFn | |
myArray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[2].reduce(([], 2) => { | |
return [].concat(2 * 2); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[4].reduce(([4], 4) => { | |
return [4].concat(4 * 2); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[6].reduce([4, 8], 6) => { | |
return [4, 8].concat(6 * 2); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[8].reduce([4, 8, 12], 8) => { | |
return [4, 8, 12].concat(8 * 2); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[2, 4, 6, 8].map(item => item * 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment