Skip to content

Instantly share code, notes, and snippets.

@evandrojr
Last active February 11, 2017 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandrojr/264af8123a78c6e63df16ce3e7c862ad to your computer and use it in GitHub Desktop.
Save evandrojr/264af8123a78c6e63df16ce3e7c862ad to your computer and use it in GitHub Desktop.
Using arrow functions
const dragonEvents = [
{type: 'attack', value: 40, victim: 'dorkman'},
{type: 'sleep', value: 10},
{type: 'attack', value: 20, victim: 'aquaman'},
{type: 'attack', value: 40, victim: 'dorkman'},
]
const v = 'dorkman';
const field = 'victim';
const selectVictim = (e) => e[field] === v;
const selectAttack = e => e.type === 'attack';
const initialValue = 9
const summer = (total, v) => ( total + v) ;
const summerInitialValue = (initialValue) => (summer, initialValue)
const myReduce = (reducer, initialValue) => (summer, initialValue)
const totalDamageOnDorkman = dragonEvents
.filter(selectAttack)
.filter(selectVictim)
.map(e => e.value)
.reduce(summer,4) //works
// Does not work .reduce(summerInitialValue)
// Does not work [myReduce]
console.log(totalDamageOnDorkman)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment