Skip to content

Instantly share code, notes, and snippets.

@denizozger
Created December 13, 2016 18:09
Show Gist options
  • Save denizozger/02e327e567b36a3384368c22baaacca1 to your computer and use it in GitHub Desktop.
Save denizozger/02e327e567b36a3384368c22baaacca1 to your computer and use it in GitHub Desktop.
Ramda cheatsheet
// Replace this:
for (const value of myArray) {
console.log(value)
}
// with:
forEach(value => console.log(value), myArray)
const double = x => x * 2
map(double, [1, 2, 3])
const isEven = x => x % 2 === 0
filter(isEven, [1, 2, 3, 4]) // --> [2, 4]
reject(isEven, [1, 2, 3, 4]) // --> [1, 3]
const isOdd = complement(isEven)
find(isOdd, [1, 2, 3, 4]) // --> 1
const add = (accum, value) => accum + value
reduce(add, 5, [1, 2, 3, 4])
/*
*** 2 ***
*/
const wasBornInCountry = person => person.birthCountry === OUR_COUNTRY
const wasNaturalized = person => Boolean(person.naturalizationDate)
const isOver18 = person => person.age >= 18
const isCitizen = person => wasBornInCountry(person) || wasNaturalized(person)
const isEligibleToVote = person => isOver18(person) && isCitizen(person)
const isCitizen = either(wasBornInCountry, wasNaturalized)
const isEligibleToVote = both(isOver18, isCitizen)
const multiply = (a, b) => a * b
const addOne = x => x + 1
const square = x => x * x
const operate = (x, y) => {
const product = multiply(x, y)
const incremented = addOne(product)
const squared = square(incremented)
return squared
}
operate(3, 4) // => ((3 * 4) + 1)^2 => (12 + 1)^2 => 13^2 => 169
const operate = pipe(
multiply,
addOne,
square
)
const operate = (x, y) => square(addOne(multiply(x, y)))
const operate = compose(
square,
addOne,
multiply
)
/*
*** 3 ***
*/
const publishedInYear = (book, year) => book.year === year
const titlesForYear = (books, year) => {
const selected = filter(book => publishedInYear(book, year), books)
return map(book => book.title, selected)
}
// Full function version:
function publishedInYear(year) {
return function(book) {
return book.year === year
}
}
// Arrow function version:
const publishedInYear = year => book => book.year === year
const publishedInYear = year => book => book.year === year
const titlesForYear = (books, year) => {
const selected = filter(publishedInYear(year), books)
return map(book => book.title, selected)
}
const publishedInYear = (book, year) => book.year === year
const titlesForYear = (books, year) => {
const selected = filter(partialRight(publishedInYear, [year]), books)
return map(book => book.title, selected)
}
const publishedInYear = curry((year, book) => book.year === year)
const titlesForYear = (books, year) =>
pipe(
filter(publishedInYear(year)),
map(book => book.title)
)(books)
const publishedInYear = curry((year, book) => book.year === year)
const titlesForYear = curry((year, books) =>
pipe(
filter(publishedInYear(year)),
map(book => book.title)
)(books)
)
/*
*** 3 ***
*/
const square = x => multiply(x, x)
const operate = pipe(
multiply,
inc,
square
)
const wasBornInCountry = person => person.birthCountry === OUR_COUNTRY
const wasNaturalized = person => Boolean(person.naturalizationDate)
const isOver18 = person => person.age >= 18
const isCitizen = either(wasBornInCountry, wasNaturalized)
const isEligibleToVote = both(isOver18, isCitizen)
const wasBornInCountry = person => equals(person.birthCountry, OUR_COUNTRY)
const wasNaturalized = person => Boolean(person.naturalizationDate)
const isOver18 = person => gte(person.age, 18)
const isCitizen = either(wasBornInCountry, wasNaturalized)
const isEligibleToVote = both(isOver18, isCitizen)
const lineWidth = settings.lineWidth || 80
const lineWidth = defaultTo(80, settings.lineWidth)
const forever21 = age => age >= 21 ? 21 : age + 1
const forever21 = age => ifElse(gte(__, 21), () => 21, inc)(ageNi)
const forever21 = age => ifElse(lt(21), () => 21, inc)(age)
const forever21 = age => ifElse(gte(__, 21), always(21), inc)(age)
const alwaysDrivingAge = age => ifElse(lt(__, 16), always(16), a => a)(age)
const alwaysDrivingAge = age => ifElse(lt(__, 16), always(16), identity)(age)
const alwaysDrivingAge = age => when(lt(__, 16), always(16))(age)
const alwaysDrivingAge = age => unless(gte(__, 16), always(16))(age)
const water = temperature => cond([
[equals(0), always('water freezes at 0°C')],
[equals(100), always('water boils at 100°C')],
[T, temp => `nothing special happens at ${temp}°C`]
])(temperature)
/*
*** 4 ***
*/
const forever21 = ifElse(gte(__, 21), always(21), inc)
const alwaysDrivingAge = when(lt(__, 16), always(16))
const water = cond([
[equals(0), always('water freezes at 0°C')],
[equals(100), always('water boils at 100°C')],
[T, temp => `nothing special happens at ${temp}°C`]
])
const titlesForYear = curry((year, books) =>
pipe(
filter(publishedInYear(year)),
map(book => book.title)
)(books)
)
const titlesForYear = year =>
pipe(
filter(publishedInYear(year)),
map(book => book.title)
)
const isCitizen = person => either(wasBornInCountry, wasNaturalized)(person)
const isEligibleToVote = person => both(isOver18, isCitizen)(person)
const isCitizen = either(wasBornInCountry, wasNaturalized)
const isEligibleToVote = both(isOver18, isCitizen)
/*
*** 5 *** http://randycoulman.com/blog/2016/06/28/thinking-in-ramda-immutability-and-objects/
*/
const wasBornInCountry = person => person.birthCountry === OUR_COUNTRY
const wasNaturalized = person => Boolean(person.naturalizationDate)
const isOver18 = person => person.age >= 18
const isCitizen = either(wasBornInCountry, wasNaturalized)
const isEligibleToVote = both(isOver18, isCitizen)
const wasBornInCountry = person => equals(person.birthCountry, OUR_COUNTRY)
const wasNaturalized = person => Boolean(person.naturalizationDate)
const isOver18 = person => gte(person.age, 18)
const wasBornInCountry = person => equals(prop('birthCountry', person), OUR_COUNTRY)
const wasNaturalized = person => Boolean(prop('naturalizationDate', person))
const isOver18 = person => gte(prop('age', person), 18)
const wasBornInCountry = person => equals(OUR_COUNTRY, prop('birthCountry', person))
const wasNaturalized = person => Boolean(prop('naturalizationDate', person))
const isOver18 = person => gte(prop('age', person), 18)
const wasBornInCountry = person => equals(OUR_COUNTRY)(prop('birthCountry', person))
const wasNaturalized = person => Boolean(prop('naturalizationDate', person))
const isOver18 = person => gte(__, 18)(prop('age', person))
const wasBornInCountry = person => equals(OUR_COUNTRY)(prop('birthCountry')(person))
const wasNaturalized = person => Boolean(prop('naturalizationDate')(person))
const isOver18 = person => gte(__, 18)(prop('age')(person))
const wasBornInCountry = person => compose(equals(OUR_COUNTRY), prop('birthCountry'))(person)
const wasNaturalized = person => compose(Boolean, prop('naturalizationDate'))(person)
const isOver18 = person => compose(gte(__, 18), prop('age'))(person)
const wasBornInCountry = compose(equals(OUR_COUNTRY), prop('birthCountry'))
const wasNaturalized = compose(Boolean, prop('naturalizationDate'))
const isOver18 = compose(gte(__, 18), prop('age'))
pick(['name', 'age'], person)
has('name', person)
path(['address', 'zipCode'], person)
propOr('<Unnamed>', 'name', person)
const updatedPerson = assoc('name', 'New name', person)
const updatedPerson = assocPath(['address', 'zipcode'], '97504', person)
const updatedPerson = dissoc('age', person)
dissocPath(['address', 'zipCode'], person)
const updatedPerson = omit(['age', 'birthCountry'], person)
const nextAge = compose(inc, prop('age'))
const celebrateBirthday = person => assoc('age', nextAge(person), person)
const celebrateBirthday = evolve({ age: inc })
function f(a, b, options = {}) {
const defaultOptions = { value: 42, local: true }
const finalOptions = merge(defaultOptions, options)
}
/*
*** 5 *** http://randycoulman.com/blog/2016/07/05/thinking-in-ramda-immutability-and-arrays/
*/
const numbers = [10, 20, 30, 40, 50, 60]
nth(3, numbers) // => 40 (0-based indexing)
nth(-2, numbers) // => 50 (negative numbers start from the right)
slice(2, 5, numbers) // => [30, 40, 50] (see below)
contains(20, numbers) // => true
head(numbers) // => 10
tail(numbers) // => [20, 30, 40, 50, 60]
last(numbers) // => 60
init(numbers) // => [10, 20, 30, 40, 50]
take(3, numbers) // => [10, 20, 30]
takeLast(3, numbers) // => [40, 50, 60]
insert(3, 35, numbers) // => [10, 20, 30, 35, 40, 50, 60]
append(70, numbers) // => [10, 20, 30, 40, 50, 60, 70]
prepend(0, numbers) // => [0, 10, 20, 30, 40, 50, 60]
update(1, 15, numbers) // => [10, 15, 30, 40, 50, 60]
concat(numbers, [70, 80, 90]) // => [10, 20, 30, 40, 50, 60, 70, 80, 90]
remove(2, 3, numbers) // => [10, 20, 60]
without([30, 40, 50], numbers) // => [10, 20, 60]
drop(3, numbers) // => [40, 50, 60]
dropLast(3, numbers) // => [10, 20, 30]
update(2, multiply(10, nth(2, numbers)), numbers) // => [10, 20, 300, 40, 50, 60]
adjust(multiply(10), 2, numbers)
// http://randycoulman.com/blog/2016/07/12/thinking-in-ramda-lenses/
const person = {
name: 'Randy',
socialMedia: {
github: 'randycoulman',
twitter: '@randycoulman'
}
}
const nameLens = lensProp('name')
const twitterLens = lensPath(['socialMedia', 'twitter'])
view(nameLens, person) // => 'Randy'
set(twitterLens, '@randy', person)
// => {
// name: 'Randy',
// socialMedia: {
// github: 'randycoulman',
// twitter: '@randy'
// }
// }
over(nameLens, toUpper, person)
// => {
// name: 'RANDY',
// socialMedia: {
// github: 'randycoulman',
// twitter: '@randycoulman'
// }
// }
// http://randycoulman.com/blog/2016/07/19/thinking-in-ramda-wrap-up/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment