The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Declaratively Map Predicates to Object Properties Using Ramda where

Declaratively Map Predicates to Object Properties Using Ramda where

2:34
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.
Watch this lesson now
Avatar
egghead.io

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.

Avatar
Hozefa

Whats the advantage of using Ramda over lodash? They look to doing very similar things.

In reply to egghead.io
Avatar
Andrew Van Slaars

I'll start by saying that lodash is an awesome library, I have nothing against it. That being said, the biggest advantage Ramda has over lodash is the combination of argument order and automatic function currying. So, for something like filter, lodash takes the collection as the first argument, where ramda takes the predicate function first, and it then curries that function. So, R.filter(x => x % 2 == 0) returns a new function that you just have to pass data to. To accomplish the same thing with lodash, it would look like this: _.partial(_.filter, _, (x) => x%2==0) where you need to use partial application to return a partially applied version of filter function and a placeholder to account for the fact that I don't have the data yet and it is expected as the first argument to filter.

Having curried functions that can be used on different data contributes to building small, reusable functions and it also makes it easier to put together functional pipelines or compositions while keeping the code readable.

You can accomplish the same things with the functional programming variant of lodash, lodash/fp. Using lodash/fp gets you most of the same capabilities, but it doesn't offer lenses, and lenses can be very powerful.

Hope this helps.

In reply to Hozefa
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?