Skip to content

Instantly share code, notes, and snippets.

@pertrai1
Created March 11, 2019 04:30
Show Gist options
  • Save pertrai1/2c397978f374987f4970b1c6b8986599 to your computer and use it in GitHub Desktop.
Save pertrai1/2c397978f374987f4970b1c6b8986599 to your computer and use it in GitHub Desktop.
Map Reduce Example
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);
(fn, arr) => arr.reduce((acc, item) => {
return acc.concat(fn(item));
}, []);
item => item * 2
reduceFn
myArray
[2].reduce(([], 2) => {
return [].concat(2 * 2);
});
[4].reduce(([4], 4) => {
return [4].concat(4 * 2);
});
[6].reduce([4, 8], 6) => {
return [4, 8].concat(6 * 2);
});
[8].reduce([4, 8, 12], 8) => {
return [4, 8, 12].concat(8 * 2);
});
[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