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.
If you were to write a function to check if the first value in the array was the largest value of the array, it would look something like this where we'll say isFirstBiggest is a function that takes in an array, and then it does a comparison where we check if the first item is equal to the array sorted.
We have a comparator function ((a,b)=>b-a), so that the bigger one always goes to the front, and then take the first of that. I hit save here, and you can see that (shouldBeTrue)) is true and (shouldBeFalse)) is false, because six is the largest and three is not the largest.
Now, with ramda where you see this scenario of one argument being used twice in this function, it usually indicates you want to bring in something like converge. I'll import converge from 'ramda', and then converge essentially allows you to get rid of this argument here.
I'm going to drop this down a couple of lines and comment it out. I'll say converge, and these two things converge on an equals function or this comparison here. I'm going to bring in equals. I'll say converge equals, and then an array of functions.
The first function will be this one which was the array, and then returns the first item, and then the second function was this one where we have the array and it returns the first item after it's sorted. Now when I hit save, you'll see we have the same true and false.
I'll break this down to new lines, just so you can see it a bit better. Again it's this, and this is here and here, and they're converging into equals meaning they are combining into this. Now, ramda already has a function to get the first item off of an array. I can just bring that one in, and it's called head. I'll say head.
We can also use head for this as a function, while I get rid of that and evoke this with head. We're still at the exact same thing, true and false, and there's actually a sort and a descend function as well from ramda. I'll bring those in, sort, descend, and then we'll need to descend on the identity.
Meaning, the property to check inside of here as just the number in the array itself. That will look like sort invoked in with descend, invoked in with identity, and then we'd have to pass in the array right here, and we're back at true and false again.
Now, finally to get rid of the argument here and make this completely point free, we should compose these functions instead of nesting them like this. I'll go ahead and bring in compose, and now I can get rid of this, and I can get rid of this.
I'll say compose(head, then the sorting function. It'll invoke this with the array, and then pass that over to head. The result of that will go into equals, and compare with the result of this going into equals, and now we have the same thing true and false.
Now, if you wanted you can take compose and name it something like biggestItem, and that can be a function biggestItem, and that will still work. It's just that now with converge and this point-free style no longer needing to rely on this array that we're passing in, we can swap functions in and out interchangeably, and we have more flexibility around the way that we use these.
Again, converge is two functions. This being a function and then this being a function, will be this one and this one meeting together. In another function you'd have to think of these equal signs as a function which I put in there, and then getting the result of that which would be returned just like this function would return true or false based on this expression.