1. 22
    Refactor to a Point Free Function with Ramda's useWith Function
    3m 41s
⚠️ This lesson is retired and might contain outdated information.

Refactor to a Point Free Function with Ramda's useWith Function

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

Social Share Links

Send Tweet

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.

[00:01] I've imported Ramda and pulled in the find and prop equal functions using destructuring. I have an array of country objects and a function called get country that we need to implement. This function will accept a country code and our list of countries and returned the matching object.

[00:15] To get started with get country, I'm going to come down here and I'm going to define my parameters which are going to be country code and our list of countries. Now I'm going to replace the return empty object and we'll implement this using Ramda's find function.

[00:31] Find is going to take a predicate, and for that we're going to use our prop equal function, and the property we want to match on is country code from our object. The value we want to compare that to is the country code value that's getting passed into our function.

[00:49] Then our second argument is going to be our list of countries, which in this case we called "List." If I save this and jump into the terminal I can run it and we'll see that we get back the desired country.

[01:04] This function does what we need, but I'd like to make it point free. Meaning, I want to remove the need for the name parameter CC and list in this function. In order to do this we're going to need to import a couple more things from Ramda.

[01:17] The first one is a function called useWith. Then we want the identity function. Then we can come back down here and refactor our function. I'm going to start by duplicating this line so we have the old one as a point of reference. I'm going to come down here.

[01:34] The first thing I'm going to do is remove these parameters because the whole point of this refactor is to get rid of the named data points. I'm going to call useWith. UseWith is going to take a function, which in this case is going to be find, and then it's going to take a list or an array of transformation functions.

[01:53] What I want to do first is take this prop equals and I'm going to move that into my array of transformation functions with just the first argument. Then I'll get rid of these two. Then my second transformation is going to be identity.

[02:10] What's going to happen is this is going to return a function into get country and it's going to take our two arguments, my country code string and my list of countries, and it will take this first argument, pass it into prop equals. Which is going to result in a function that has the property name and the value to compare it against.

[02:30] It's going to take that and pass it in as the first argument to find. It will basically duplicate this part of our old function. Then it's going to take our second argument, pass it to the second transformation function which in this case is just identity.

[02:45] It's going to take the value and return it and pass that into find as find's second argument. That's all it takes to take that old function that had the extra data floating around and make it point free.

[02:57] Now I can save this and I can jump into the terminal. If I run this I'll get the same result that I got before. I'll get back US with the flag. If I change my parameters down here I can save it again. Run it again and it still works as expected, but we've removed the need for the extra named parameters in our function.

[03:19] I used the identity function here to take the country's argument and pass it through to find, but useWith will do this by default. Any argument that doesn't line up with the transformation function will automatically be passed directly to the function that we pass it as its first argument.

[03:35] I like to use identity because I think it keeps it very clear, but it's completely optional here.