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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
I like to use identity because I think it keeps it very clear, but it's completely optional here.