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.
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 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.
Sorting an array of objects by multiple fields can be tricky. Even worse, the code can be tough to follow and making changes in the future is complicated by conditional logic in the sorting function. In this lesson we'll see how to simplify this using Ramda's
sortWith along with
descending. We'll sort an array of objects by multiple fields and the code will be readable and dead simple to change.
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.
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.
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.
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 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.
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.
fromPairs functions, along with the crucial
We don't always control the data we need in our applications, and that means we often find ourselves massaging and transforming our data. In this lesson, we'll learn how to transform objects in a declarative way using ramda's evolve function.
In this lesson we'll take an array of objects and map it to a new array where each object is a subset of the original. We'll look at multiple ways to accomplish this, refactoring our code into a simple and easy to read function using Ramda's
Most of the functions offered by the ramda library are curried by default. Functions you've created or that you've pulled in from another library may not be curried. Ramda's
curryN functions allow you to take a non-curried function and use it as a curried functions. In the case where you have a manually curried function and you want to just call it like a normal function, you can use
uncurryN to get back a function that accepts all of the arguments at once.
When you want to build your logic with small, composable functions you need a functional way to handle conditional logic. You could wrap ternary expressions and if/else statements in functions, handling all of the concerns around data mutation yourself, or you could leverage the conditional functions supplied by Ramda. In this lesson, we'll cover several of Ramda's conditional functions:
Sometimes you need to filter an array of objects or perform other conditional logic based on a combination of factors. Ramda's
where function gives you a concise way to declaratively map individual predicates to object properties, that when combined, cover the various facets of your conditions. In this lesson, we'll look at how this powerful function can be used for scenarios requiring a complex predicate function.
We'll learn how to get a subset of an array by specifying items to include with filter, or items to exclude using reject. We'll also look at how to get the results from both filter and reject, neatly separated with partition.