Once you’ve gotten used to the idea of passing functions to other functions, you might start to find situations where you want to combine sereral functions together.
Ramda provides several functions for doing simple combinations.
Complement
Ramda provides a higher-order-function,complement, that takes another function and return a new function that returns true when the original function returns a falsy value, and false when the original function returns a truthy value.
As we start writing small functional building blocks and combining them, we find that we have to write a lot of functions that wrap JavaScript’s operators such as arithmetic, comparison, logic, and control flow. This can feel tedious, but Ramda has our back.
There are many different ways to divide up the programming language/style landscape. There’s static typing vs dynamic typing, interpreted languages vs compiled languages, low-level vs high-level, etc.
Another such division is imperative programming vs declarative programming.
We can make the functions more declarative using equals and gte.
Prop
Fortunately, Ramda can help us out.It provides the prop function for accesing properties of an object.
Pick
Where prop reads a single property from an object and returns the value, p ick reads multiple properties from an object and returns a new object with just those properties.
It also provides functions for accesing all-but-the-first element tail,all-but-the-last element init,the first N elements take(N), and the last N elments takeLast(N).
Ramda provides a more general tool for performing the operations such as read, update, and transform object properties and array elements in a declarative, immutable way, the lens.
What is a Lens?
A lens combines a “getter” function and a “setter” function into a single unit. Ramda provides a set of functions for working with lenses.
We can think of a lens as something that focuses on a specific part of a larger data structure.
How Do I Create a Lens?
The most generic way to create a lens in Ramda is with the lens function. lens takes a getter function and a setter function and returns the new lens.
Ramda has some underlying principles that drive its API:
Data last: Almost all of the functions take the data parameter as the last parameter.
Currying: Almost every function in Ramda is “curried”. That is, you can call a function with only a subset of its required arguments, and it will return a new function that takes the remaining arguments. Once all of the arguments are provided, the original function is invoked.