Last active
January 16, 2018 09:07
-
-
Save walkermatt/0a26a9bfe3b1dcf45944952fe48133ba to your computer and use it in GitHub Desktop.
Ramda stuff
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 states = [ | |
{symbol: 'CT', name: 'Connecticut', pop: 3574097}, | |
{symbol: 'ME', name: 'Maine', pop: 1328361}, | |
{symbol: 'MA', name: 'Massachusetts', pop: 6547629}, | |
{symbol: 'NH', name: 'New Hampshire', pop: 1316470}, | |
{symbol: 'RI', name: 'Rhode Island', pop: 1052567}, | |
{symbol: 'VT', name: 'Vermont', pop: 623741}, | |
]; | |
var notSmall = (state) => state.pop >= 2000000; | |
R.reduce((accum, state) => notSmall(state) ? | |
R.assoc(state.symbol, state.pop, accum) : | |
accum, | |
{}, states); //=> {"CT": 3574097, "MA": 6547629} | |
R.pipe( | |
R.filter(notSmall), | |
R.map(R.props(['symbol', 'pop'])), | |
R.fromPairs)(states) //=> {"CT": 3574097, "MA": 6547629} | |
R.reduce((accum, state) => notSmall(state) ? R.assoc(state.symbol, state.pop, accum) : accum, {}, states); | |
R.pipe( | |
R.filter(notSmall), | |
R.map(R.props(['symbol', 'pop'])), | |
R.fromPairs)(states); | |
R.fromPairs(R.map(R.props(['symbol', 'pop']), R.filter(notSmall, states))) | |
R.fromPairs([[ 'CT', 3574097 ]]) | |
R.pipe(R.props(['symbol', 'pop']), R.fromPairs)([ 'CT', 3574097 ]); | |
R.map(R.pipe(R.props(['symbol', 'pop']), R.fromPairs), R.filter(notSmall, states)); | |
// Path | |
R.path(['a', 'b'], {a: {b: 2}}); | |
// Combining functions | |
const isEven = x => x % 2 === 0 | |
R.find(isEven, [1, 2, 3, 4]) | |
R.find(R.complement(isEven), [1, 2, 3, 4]) | |
R.filter(isEven, [1, 2, 3, 4]) | |
R.filter(R.complement(isEven), [1, 2, 3, 4]) | |
// Both/ Either/ Curry | |
var wasBornInCountry = R.curry((country, person) => person.birthCountry === country) | |
wasBornInCountry('GB') //=> [Function] | |
wasBornInCountry('GB', {birthCountry: 'GB'}) //=> true | |
wasBornInCountry('GB')({birthCountry: 'GB'}) //=> true | |
wasBornInCountry('GB')({birthCountry: 'IE'}) //=> false | |
var wasNaturalized = person => Boolean(person.naturalizationDate) | |
wasNaturalized({birthCountry: 'IE', naturalizationDate: '2014-01-01'}) //=> true | |
wasNaturalized({}) //=> false | |
var isOver18 = person => person.age >= 18 | |
isOver18({age: 17}) //=> false | |
isOver18({age: 18}) //=> true | |
isOver18({age: 19}) //=> true | |
var isCitizen = R.curry((country, person) => R.either(wasBornInCountry(country), wasNaturalized)(person)) | |
isCitizen('GB')({birthCountry: 'IE', naturalizationDate: '2014-01-01'}) | |
isCitizen('GB', {birthCountry: 'IE', naturalizationDate: '2014-01-01'}) | |
isCitizen('GB', {birthCountry: 'GB'}) | |
var isEligibleToVote = (country, person) => R.both(isCitizen(country), isOver18)(person) | |
isEligibleToVote('GB', {birthCountry: 'GB', age: 19}) //=> true | |
isEligibleToVote('GB', {birthCountry: 'GB', age: 17}) //=> false | |
isEligibleToVote('GB', {birthCountry: 'IE', age: 19}) //=> false | |
isEligibleToVote('GB', {birthCountry: 'IE', age: 19, naturalizationDate: '2014-01-01'}) //=> true | |
var isEligibleToVoteGB = R.curry(isEligibleToVote)('GB'); | |
isEligibleToVote('GB', {birthCountry: 'GB', age: 19}) //=> true | |
isEligibleToVoteGB({birthCountry: 'IE', age: 19}) //=> false | |
isEligibleToVoteGB({birthCountry: 'IE', age: 19, naturalizationDate: '2014-01-01'}) //=> true | |
var gt10 = x => x > 10; | |
var even = x => x % 2 === 0; | |
var f = R.either(gt10, even); | |
f(101); //=> true | |
f(8); //=> true | |
f(7) //=> false | |
// Define a curried greater than function | |
var gt = R.curry((x, n) => n > x); | |
gt(10, 11) //=> true | |
gt(10, 10) //=> false | |
gt(10, 9) //=> false | |
// Redefine the greater than 10 function by only supplying the first argument | |
// to our curried greater than function which will return a new function which | |
// accepts a single argument (the number to test) | |
var gt10 = gt(10) | |
gt10 | |
var f2 = R.either(gt10, even); | |
f2(101); //=> true | |
f2(8) //=> true | |
f2(7) //=> false | |
var f3 = R.both(gt10, even); | |
f3(101); //=> false | |
f3(8) //=> false | |
f3(7) //=> false | |
f3(102); //=> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment