Last active
February 11, 2017 21:37
-
-
Save evandrojr/264af8123a78c6e63df16ce3e7c862ad to your computer and use it in GitHub Desktop.
Using arrow functions
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 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