What it is
Ramda is a practical functional library for JavaScript programmers. It provides a suite of utility functions for functional programming, emphasizing immutability, currying, and composition.
Ramda provides functions for working with arrays, objects, strings, and functions in a functional style. Its utilities support currying, composition, and point-free programming.
Installation
npm install ramdaGetting started
The smallest useful thing you can do with it, and what each part means.
import R from 'ramda';
const numbers = [1, 2, 3];
const doubled = R.map(x => x * 2, numbers);
console.log(doubled); // [2, 4, 6]import R from 'ramda';
const numbers = [1, 2, 3, 4];
const evens = R.filter(x => x % 2 === 0, numbers);
console.log(evens); // [2, 4]Advanced usage
Where the library earns its place over a simpler alternative.
import R from 'ramda';
const add1 = x => x + 1;
const double = x => x * 2;
const addThenDouble = R.compose(double, add1);
console.log(addThenDouble(3)); // 8import R from 'ramda';
const add = (a, b) => a + b;
const curriedAdd = R.curry(add);
const add5 = curriedAdd(5);
console.log(add5(10)); // 15import R from 'ramda';
const obj = { a: { b: 1 } };
const updated = R.assocPath(['a', 'b'], 42, obj);
console.log(updated); // { a: { b: 42 } }import R from 'ramda';
const user = { name: 'Alice', address: { city: 'NY' } };
const cityLens = R.lensPath(['address', 'city']);
const updatedUser = R.set(cityLens, 'LA', user);
console.log(R.view(cityLens, updatedUser)); // 'LA'Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- TypeError: value.map is not a function
- Ensure the input to array functions like `R.map` or `R.filter` is actually an array.
- Lens operations not updating correctly
- Make sure you use `R.set` to update values and `R.view` to read, since Ramda functions are immutable.
Best practices
- Use point-free style and function composition for cleaner code.
- Favor immutability and avoid mutating input data.
- Leverage currying and partial application to simplify reusable functions.
- Use lenses for safe manipulation of nested objects.
- Combine Ramda functions with native JavaScript for optimal performance and readability.
Background
Why it exists, and what it was reacting to.
Ramda was created to help developers write cleaner and more maintainable code using functional programming principles. Unlike other utility libraries, Ramda's functions are automatically curried and designed to be data-last, making composition simple and intuitive.
