Functional Programming with Ramda.js
Using the map method of an array is a great way to create new data from an existing array in an immutable way and without writing any loops. The problem is that you need to call it on an existing array instance. In the lesson we'll see how to build our own reusable
map utility function using manual currying and then we'll replace it with Ramda's
In this lesson we'll see how Ramda's
pathOr functions can be used to safely access a deeply nested property from an object while avoiding the dreaded checks for
undefined at each new property in the desired path.
In this lesson, we'll use
Promise.all to get an array that contains the resolved values from multiple promises. Then we'll see how we can use Ramda to convert that array of values into a single object using
fromPairs. Then we'll refactor to use
In this lesson, we'll look at how we can use Ramda's
constructN functions to take methods of an object and turn them into reusable utility functions that are curried and accept their object as the last argument. We'll convert a dot-chained string of jQuery methods and create a composed function that can be applied to multiple elements.
Promise chains can be a powerful way to handle a series of transformations to the results of an async call. In some cases, additional promises are required along the way. In cases where there are no new promises, function composition can reduce the number of dot chained
thens you need. In this lesson, we'll look at how to take a promise chain, and reduce it down with function composition.
Naming things is hard and arguments in generic utility functions are no exception. Making functions "tacit" or "point free" removes the need for the extra parameter names and can make your code cleaner and more succinct. In this lesson, we'll create a normal, "pointed" function and then use ramda's
useWith function to refactor our way to point-free bliss.
In this lesson, we'll grab arrays of values from other arrays, resulting in a nested array. From there, we'll look at multiple ways to flatten the array structure using composition with
unnest and then refactoring to use
flatMap. Finally, we'll add Ramda's
uniq function to remove duplicate values.
Functional composition is a great way to write declarative code that reads well. But sometimes, you need to get insight into what's happening at different steps in your composition. This is where
tap comes in. With tap, we can run code that generates side-effects, like logging to the console, and our data will continue to be passed along, untouched.
When doing comparisons inside of functions, you end of relying heavily on the argument passed into the function. Ramda's
converge allows you to do comparisons in a Point-Free style allowing you more flexibility with composing and constructing functions. This lesson walks through refactoring a function to Point-Free style using Ramda's Converge.
You can really unlock the power of ramda (and functional programming in general) when you combine functions. Counting words in a string may seem like a relatively difficult task, but ramda makes it easy by providing a
countBy function. This lesson walks through using the
countBy to count words in a string.
In this lesson we'll take some existing code and refactor it using some functions from the Ramda library, most notably,
converge. When we're done, we'll have taken a function with a couple of local variables and parameter references and converted it into more streamlined "point-free" or "tacit" functions.
Handling your logic with composable functions makes your code declarative, leading to code that's easy to read and easy to test. Breaking that up to wrap some risky function in a
try/catch block introduces imperative code and makes it harder to maintain that declarative approach. With Ramda's
tryCatch function, you can handle errors right in the middle of a composition and leave your code clean and functional. We'll also see how you can use
propOr to avoid common "cannot find X of undefined" errors.
In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. We'll create a lens to focus on the property we want to target and use
over to apply the existing state value to a utility function and we'll get back a new state that will be reflected in our rendered UI.
In this lesson we'll take a stateful React component and look at how we can refactor our
setState calls to use an updater function and then leverage Ramda's
evolve function to make our updater function a reusable utility that isn't tied to the React API.