Skip to content

Instantly share code, notes, and snippets.

@robertocarroll
Forked from mpj/1hello.md
Last active November 19, 2016 07:58
Show Gist options
  • Save robertocarroll/b3063420a7f9fc2f4538cb1745a4fb07 to your computer and use it in GitHub Desktop.
Save robertocarroll/b3063420a7f9fc2f4538cb1745a4fb07 to your computer and use it in GitHub Desktop.
Arrow functions
const dragonEvents = [
{ type: 'attack', value: 12, target: 'player-dorkman' },
{ type: 'yawn', value: 40 },
{ type: 'eat', target: 'horse' },
{ type: 'attack', value: 23, target: 'player-fluffykins' },
{ type: 'attack', value: 12, target: 'player-dorkman' },
]
const totalDamageOnDorkman = dragonEvents
.filter(function (event) {
return event.type === 'attack'
})
.filter(function (event) {
return event.target === 'player-dorkman'
})
.map(function(event) {
return event.value
})
.reduce(function(prev, value) {
return (prev || 0) + value
})
// I like the bit where he creates re-usable functions
const reduceTotal = (prev, x) => (prev || 0) + x
const isAttack = e => e.type === 'attack'
const totalDamageOnDorkman = dragonEvents
.filter(isAttack)
.filter(e => e.target === 'player-dorkman')
.map(e => e.value)
.reduce(reduceTotal)
const totalDamageOnDorkman = dragonEvents
.filter(e => e.type === 'attack')
.filter(e => e.target === 'player-dorkman')
.map(e => e.value)
.reduce((prev, x) => (prev || 0) + x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment