Created
September 24, 2019 10:36
-
-
Save iamaamir/442b54f4aa8f45893f2b6041e6438708 to your computer and use it in GitHub Desktop.
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
// Sum Numbers over an array | |
const sum = array => array.reduce((acum, number) => { | |
return acum + number; | |
}, 0); | |
console.log('01', { sum: sum([ 1, 5, 9 ]) }); | |
// Sum Numbers multiplied by a factor | |
const sumProduct = array => array.reduce((acum, pair) => { | |
const [ number, factor ] = pair; | |
return acum + number * factor; | |
}, 0); | |
console.log('02', { sumProduct: sumProduct([ [ 1, 1 ] , [ 5, 3 ], [ 9, 2 ] ]) }); | |
// Get the max value | |
const maxNumber = array => array.reduce((acum, number) => { | |
if (!acum) return number; | |
if (acum < number) return number; | |
return acum; | |
}, null); | |
console.log('03', { maxNumber: maxNumber([ 1, 5, 9 ]) }); | |
// Implement "map" function over an array | |
const map = (array, fn) => array.reduce((acum, elem) => { | |
return [ ...acum, fn(elem) ]; | |
}, []); | |
console.log('04', { map: map([ 1, 2, 3 ], x => x * 2) }); | |
// Implement "filter" function over an array | |
const filter = (array, fn) => array.reduce((acum, elem) => { | |
if (!fn(elem)) return acum; | |
return [ ...acum, elem ]; | |
}, []); | |
console.log('05', { filter: filter([ 1, 2, 3 ], x => x >= 2) }); | |
// Filter (first) and then Map over the array | |
const filterMap = (array, fnFilter, fnMap) => array.reduce((acum, elem) => { | |
if (!fnFilter(elem)) return acum; | |
return [ ...acum, fnMap(elem) ]; | |
}, []); | |
console.log('06', { filterMap: filterMap([ 1, 2, 3 ], x => x >= 2, x => x * 2) }); | |
// Implement "flatMap" function over an array | |
const flatMap = (array, fn) => array.reduce((acum, elem) => { | |
if (Array.isArray(elem)) return [ ...acum, ...elem.map(fn) ]; | |
return [ ...acum, fn(elem) ]; | |
}, []); | |
console.log('07', { flatMap: flatMap([ [ 1, 2 ], 3, [ 4, 5 ] ], x => x * 2) }); | |
// Implement "unique" function over an array | |
const unique = array => array.reduce((acum, elem) => { | |
if (acum.includes(elem)) return acum; | |
return [ ...acum, elem ]; | |
}, []); | |
console.log('08', { unique: unique([ 1, 1, 3, 5, 3, 9 ]) }); | |
// Implement "sort" function over an array, even with non-unique elements | |
const sort = array => array.reduce((acum, elem) => { | |
const lesserOrEqual = acum.filter(item => item <= elem); | |
const greater = acum.filter(item => item > elem); | |
return [ ...lesserOrEqual, elem, ...greater ]; | |
}, []); | |
console.log('09', { sort: sort([ 3, 1, 5, 9, 4, 2, 4, 1 ]) }); | |
// Implement "reverse" function over an array | |
const reverse = array => array.reduce((acum, elem) => { | |
return [ elem, ...acum ]; | |
}, []); | |
console.log('10', { reverse: reverse([ 9, 5, 2, 1 ]) }); | |
// Object to Array | |
const objToArray = obj => Object.keys(obj).reduce((acum, key) => { | |
return [ | |
...acum, | |
{ key, value: obj[key] }, | |
]; | |
}, []); | |
console.log('11', { objToArray: objToArray({ name: 'Jose', age: 44, dog: 'Apolo' }) }); | |
// Array to Object | |
const arrayToObj = array => array.reduce((acum, elem) => { | |
const { id, ...attrs } = elem; | |
return { | |
...acum, | |
[id]: attrs, | |
}; | |
}, {}); | |
console.log('12', { arrayToObj: arrayToObj([ | |
{ id: 'e1', name: 'Jose', type: 'human' }, | |
{ id: 'e2', name: 'Apolo', type: 'dog' }, | |
{ id: 'e3', name: 'Chess', type: 'passion' }, | |
]) }); | |
// "Augment" an array by adding elements, useful for example for list organized by sections | |
const sections = [ | |
{ num: 1, name: 'Chess Pieces' }, | |
{ num: 2, name: 'Chess World Champions' }, | |
]; | |
const items = [ | |
{ section: 1, name: 'Queen' }, | |
{ section: 1, name: 'King' }, | |
{ section: 1, name: 'Pawn' }, | |
{ section: 2, name: 'Magnus Carlsen' }, | |
{ section: 2, name: 'Garry Kasparov' }, | |
{ section: 2, name: 'Bobby Fischer' }, | |
{ section: 2, name: 'Alexander Alekhine' }, | |
]; | |
const augmented = items.reduce((acum, elem, index) => { | |
const findSection = sections.find(item => item.num === elem.section); | |
const addSection = index === 0 || index > 0 && elem.section !== items[index - 1].section; | |
if (addSection) acum.push({ type: 'section', name: findSection.name }); | |
return [ | |
...acum, | |
{ type: 'item', name: elem.name }, | |
]; | |
}, []); | |
console.log('bonus', { augmented }); | |
// Process promise chain with reduce | |
const processor = steps => steps.reduce((p, fn) => { | |
let proceed = true; | |
return p | |
.then(res => { | |
if (proceed) return fn(res); | |
}) | |
.catch(err => { | |
proceed = false; | |
console.log(err); | |
}); | |
}, Promise.resolve({})); | |
const aPromise = text => { | |
return new Promise(resolve => setTimeout(() => { | |
console.log('A PROMISE: ', text); | |
resolve(text); | |
}, 1000)); | |
}; | |
const task1 = [ | |
() => aPromise('step1'), | |
res => aPromise(`${res} - step2`), | |
res => aPromise(`${res} - step3`), | |
] | |
processor(task1); | |
// ref => https://itnext.io/powerful-js-reduce-function-58cf47edcb8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment