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.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Eliminate Function Arguments (Point-Free Style) with Ramda's Converge - js Video ...
本站链接:http://www.codesec.net/view/531090.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(49)