1. 1
    Refactor to Point Free Functions with Ramda using compose and converge
    3m 33s
⚠️ This lesson is retired and might contain outdated information.

Refactor to Point Free Functions with Ramda using compose and converge

Andy Van Slaars
InstructorAndy Van Slaars
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 2 years ago

In this lesson we'll take some existing code and refactor it using some functions from the Ramda library, most notably, compose and 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.

[00:00] I've included the Ramda library and I have a person object defined. I've also defined two functions, one to take an ID value and return a URL string that includes that ID and another that takes the person object, gets a URL using the first function, and returns a new person complete with an avatar property. If I jump over to the terminal and I run this, we'll see that everything works as expected.

[00:20] Let's take a look at how we can refactor this code. There's a problem with how I'm calling generate URL. If there's no ID value in the object passed in, we'll get back undefined. But it would be better to show a default image if the ID is undefined.

[00:32] Let's come up here and refactor this. We'll use Ramda's prop or function to define our default values. We'll use the string default. Then, we'll try to get the ID. We want to get that ID off of the person object. I'll take this reference to ID out. This'll work, but we can clean this up even more using compose.

[00:52] I'm going to come over here right before generate URL. I'm going to call r.compose. I'm going to pass it in the generate URL function followed by a call to R.propor. Then, I'm going to close this off right here.

[01:06] What we've done with compose is created a function that will take your arguments and pass it in from right to left. A person will get passed into propor, will get our ID or the value default. That'll get passed to generate URL, and then our URL assignment over here'll get the resulting value of that.

[01:22] Now that we have this contained in a function, I can cut that, come up here, define this as a new function. We'll call get URL from person, assign it there, and then, we can just use that right here with our person object.

[01:41] Now that we've broken this down, we can tighten this up a little bit. If we want to take this get URL from person here and we'll cut it, we'll just replace our URL value with that, we can actually get this function down to one line.

[01:53] We'll remove this return because we're using an arrow function. I'm just going to stretch this window out a little bit so we can see the whole thing.

[02:00] We've managed to break this out into a couple of single line functions. We got rid of that URL variable. But we're left with this function that has nested function call and a couple references to this person parameter. We can clean this up and completely remove the person parameter. To do that, we'll use Ramda's converge function.

[02:16] I'm going to start by duplicating this line, commenting out the other one for reference. I'm going to come in here, and I'm going to get rid of this call to person. I'm going to replace that with a call to r.converge.

[02:27] I'm going to jump to the end of avatar. I'm going to close the parens for r.soc. This is going to give me a function that's now waiting for two arguments because all of these functions are automatically carried.

[02:38] The second argument for converge is going to be an array of transformation functions. I'm going to wrap get URL from persons square brackets. I'm going to get rid of these references to person.

[02:46] What's going to happen is our person argument's going to be passed in. It's going to be passed to each transformation function which is going to give us an array of results and that array is then going to be passed in as arguments to this first function here.

[03:00] What we need is get URL from person which is going to take a person, it'll get the ID, generate a URL. It'll pass that in as the second argument here.

[03:09] Then, we need one more transformation that's going to take person and basically just pass it along as is. For that, we can use Ramda's identity function that'll take whatever argument gets passed in and just return it.

[03:21] With all the refactoring done, let's save this. We'll jump into the terminal and we'll make sure this works. Everything's working as it did before, but we're doing this with a point free function and no extra variables are required.

Brendan Whiting
Brendan Whiting
~ 5 years ago

I don't understand the benefit here. We could have just added a default value to the id parameter in case it was null:

const generateUrl = (id = 'default') => `https://img.socialnetwork.com/avatar/${id}.png`

The way we've refactored with Ramda is looks more confusing and harder to understand. I'm not familiar with Ramda yet maybe I'll see the point by the end of the course.

Eliezer Steinbock
Eliezer Steinbock
~ 5 years ago

How is this the first episode of the series? It just jumps right in without explaining half of what is going on?

Per
Per
~ 5 years ago

Very tough for a first lesson. Watched it several times and it's still hard to grasp. End result is not very clear to read either. Must be better examples to show benefits of ramda.

Robert Pearce
Robert Pearce
~ 4 years ago

@Brendan, in your example, if generateUrl is called with null, then your result will be "https://img.socialnetwork.com/avatar/null.png", which is not what you want.

Gerald Yeo
Gerald Yeo
~ 4 years ago

With the new nullish coalescing operator, we only need to do this:

const generateUrl = (id) => `https://img.socialnetwork.com/avatar/${id ?? 'default'}.png`
Tre' Codez
Tre' Codez
~ 4 years ago

I don't understand the benefit here. We could have just added a default value to the id parameter in case it was null:

Off the cuff, I'd say composability.

Markdown supported.
Become a member to join the discussionEnroll Today