Skip to main content

Ramda

DescriptionExampleResultType
Installation
Install the Ramda librarynpm install ramdaRamda installed
Import
Import Ramda into your projectconst R = require('ramda');Ramda imported
List
Map a function over a listconst double = R.map(x => x * 2, [1, 2, 3, 4]);[2, 4, 6, 8]List
Filter a list using a predicateconst evens = R.filter(x => x % 2 === 0, [1, 2, 3, 4]);[2, 4]List
Reduce a list using a functionconst sum = R.reduce((acc, x) => acc + x, 0, [1, 2, 3, 4]);10List
Check if a list contains a valueconst has3 = R.contains(3, [1, 2, 3, 4]);trueList
Pluck values of a property from a list of objectsconst ages = R.pluck('age', [{name: 'Alice', age: 30}, {name: 'Bob', age: 25}]);[30, 25]List
Function
Create a partially applied functionconst add = (a, b) => a + b;Function
const add5 = R.partial(add, [5]);Function
Compose multiple functionsconst double = x => x * 2;Function
const square = x => x * x;Function
const doubleAndSquare = R.compose(square, double);Function
Relation
Check if a value is less than another valueconst lessThan5 = R.lt(R.__, 5);trueRelation
Check if a value is equal to another valueconst equals5 = R.equals(5);trueRelation
String
Split a string into an array of stringsconst splitBySpace = R.split(' ', 'Hello World');['Hello', 'World']String
Join an array of strings into a single stringconst joinWithComma = R.join(', ', ['Hello', 'World']);'Hello, World'String
Object
Merge two objectsconst merged = R.merge({a: 1, b: 2}, {b: 3, c: 4});{a: 1, b: 3, c: 4}Object
Get the value of a property from an objectconst value = R.prop('a', {a: 1, b: 2});1Object
Set the value of a property on an objectconst newObj = R.assoc('c', 3, {a: 1, b: 2});{a: 1, b: 2, c: 3}Object
Create a new object with only specific propertiesconst picked = R.pick(['a', 'c'], {a: 1, b: 2, c: 3});{a: 1, c: 3}Object
Logic
Create a new function that returns true if both predicates return trueconst isEven = x => x % 2 === 0;Logic
const isGt10 = x => x > 10;Logic
const isEvenAndGt10 = R.both(isEven, isGt10);Logic
Create a new function that returns true if either predicate returns trueconst isOdd = x => x % 2 !== 0;Logic
const isGt20 = x => x > 20;Logic
const isOddOrGt20 = R.either(isOdd, isGt20);Logic
Math
Add two numbersconst added = R.add(5, 3);8Math
Subtract two numbersconst subtracted = R.subtract(5, 3);2Math
Multiply two numbersconst multiplied = R.multiply(5, 3);15Math
Divide two numbersconst divided = R.divide(10, 2);5Math