Created
January 9, 2016 22:26
-
-
Save adamgibbons/9c71dc4a94c8ae27330b to your computer and use it in GitHub Desktop.
javascript composition with ramda
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
var R = require('ramda'); | |
var people = [ | |
{ | |
name: 'Adam', | |
age: 30, | |
gender: 'm', | |
party: 'd' | |
}, | |
{ | |
name: 'Brittany', | |
age: 25, | |
gender: 'f', | |
party: 'r' | |
}, | |
{ | |
name: 'Russell', | |
age: 24, | |
gender: 'm', | |
party: 'r' | |
}, | |
{ | |
name: 'Joe', | |
age: 30, | |
gender: 'm', | |
party: 'r' | |
} | |
]; | |
var target = R.compose( | |
R.reduce(addAge, 0), | |
R.filter(republicans), | |
R.filter(males) | |
); | |
console.log(target(people)); | |
function addAge(prev, current) { | |
return prev + current.age; | |
} | |
function republicans(person) { | |
return person.party === 'r'; | |
} | |
function add(prev, current, idx, array) { | |
return prev + current.age; | |
} | |
function males(person) { | |
return person.gender === 'm'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment