What is ramda?
Ramda is a functional programming library for JavaScript that makes it easy to create functional pipelines, without mutating data. It emphasizes a purer functional style, with functions that are automatically curried and composed of small, reusable, and combinable functions.
What are ramda's main functionalities?
Immutability and Side-Effect Free Functions
Ramda functions do not mutate the input data and do not cause side effects, making it easier to reason about code.
const increment = R.map(R.add(1));
const result = increment([1, 2, 3]); // [2, 3, 4]
Function Composition
Ramda provides compose and pipe functions for combining functions into new functions, facilitating functional composition.
const getNames = R.compose(R.map(R.prop('name')), R.filter(R.propEq('isActive', true)));
const users = [{name: 'Alice', isActive: true}, {name: 'Bob', isActive: false}];
const activeUserNames = getNames(users); // ['Alice']
Automatic Currying
Ramda functions are automatically curried, allowing you to easily create new functions by partially applying arguments.
const addFourNumbers = (a, b, c, d) => a + b + c + d;
const curriedAddFourNumbers = R.curry(addFourNumbers);
const f = curriedAddFourNumbers(1, 2);
const g = f(3);
const result = g(4); // 10
Data Transformation
Ramda provides a suite of tools for transforming data structures, such as arrays and objects, in a declarative and functional way.
const sortByAge = R.sortBy(R.prop('age'));
const people = [{name: 'John', age: 23}, {name: 'Jane', age: 21}];
const sortedPeople = sortByAge(people); // [{name: 'Jane', age: 21}, {name: 'John', age: 23}]
Other packages similar to ramda
lodash
Lodash is a JavaScript utility library that offers similar functionality to Ramda, such as data manipulation and functional utilities. However, Lodash is not as strictly functional as Ramda and does not emphasize currying and function composition to the same extent.
underscore
Underscore.js is another utility library that provides functional programming helpers. It is similar to Lodash and predates both Lodash and Ramda. It has a more OOP-style approach and lacks the auto-currying and functional composition features that are core to Ramda.
immutable
Immutable.js provides persistent immutable data structures which can be used in a functional programming style. Unlike Ramda, which works with plain JavaScript types, Immutable.js uses its own types and provides different APIs for manipulating these structures.
fp-ts
fp-ts is a library for typed functional programming in TypeScript. It provides type-safe functional programming structures and utilities, and it is more focused on types and type-level programming compared to Ramda.
Project Ramda
A practical functional library for Javascript programmers.
Goals
Using this library should feel as much like using Javascript as
possible. Of course it's functional Javascript, but we're not
introducting lambda expressions in strings, we're not borrowing consed
lists, we're not porting over all of the Clojure functions.
Our basic data structures will be normal Javascript objects, and our
usual collections will be Javascript arrays. We will not try to reach
the point where all the functions have only zero, one, or two arguments.
We will certainly try to keep some of the normal features of Javascript
that seem to be unusual in functional languages, including variable
length function signatures and functions as objects with properties.
Functional programming is in good part about immutable objects and
side-effect free functions. We will stick to that as much as feasible,
but will not be dogmatic about it.
As much as we can, we would like the implementation to be both clean and
elegant. But the API is king: we will sacrifice a great deal of
implementation elegance for even a slightly cleaner API.
Unlike the developers of that silly-named Eweda project, though, this
one will focus also on performance, striving for a reliable and quick
implementation over any notions of functional purity.
Documentation
Please review the annotated source code
documentation
(generated by Docco). So far,
that's the only documentation available.
The Name
Ok, so we like sheep. That's all. It's a short name, not already
taken. It could as easily have been eweda
, but then we would be
forced to say eweda lamb!, and no one wants that. For non-English
speakers, lambs are baby sheep, ewes are female sheep, and rams are male
sheep. So perhaps ramda is a grown-up lambda... but probably not.
Structure
Automatic Currying
The functions included should automatically allow for partial
application without an explicit call to lPartial. Many of these operate
on lists. A single list parameter should probably come last, which
might conflict with the design of other libraries that have strong
functional components (I'm looking at you Underscore!)
The idea is that, if foldl has this signature:
var foldl = function(fn, accum, arr) { /* ... */}
and we have this simple function:
var add = function(a, b) {return a + b;};
then, instead of having to manually call lPartial like this:
var sum = lPartial(foldl, add, 0);
var total = sum([1, 2, 3, 4]);
with ramda, we can just do this:
var sum = foldl(add, 0);
var total = sum([1, 2, 3, 4]);
So What's With Eweda?
The eweda library was written by
the developers of this library, with similar goals. But that one strove
more for implementation elegance than for practical capabilities. Ramda
is all about giving users real-world tools. Eweda can be seen more as
an academic excercise, mostly proving out what does and doesn't work, and
doing so as elegantly as possible.