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.

For example we want to find the whether the first item of an array is the biggest number:

const shouldBeTrue = [, , , , , ]
const shouldBeFalse = [, , , , ] const isFirstBiggest = (xs) => xs[] == xs.sort((a, b) => b - a)[] console.log(isFirstBiggest(shouldBeTrue)) // true
console.log(isFirstBiggest(shouldBeFalse)) // false

In the code we can see this:

const isFirstBiggest = (xs) => xs[] === xs.sort((a, b) => b - a)[]

You can find that, param 'xs' appears both on the left and right side of euqals sign.

If match this partten, we actually can use 'converge' from  Ramda.

invoked, this new function is applied to some arguments, each branching function is applied to those same arguments. The results of each branching function are passed as arguments to the converging function to produce the return value.

For example:

const shouldBeTrue = [, , , , , ]
const shouldBeFalse = [, , , , ] import {
converge, equals, head, sort, descend, identity, compose
}
from 'ramda' const biggestNumberOfArray = compose(
head,
sort(descend(identity))
);
const isFirstBiggest = converge(
equals, [
head,
biggestNumberOfArray
]
); // xs =>
// xs[0] == xs.sort((a, b) => b - a)[0] console.log(isFirstBiggest(shouldBeTrue))
console.log(isFirstBiggest(shouldBeFalse))

So in the code:

converge(
equals, [
head,
biggestNumberOfArray
]
)

converge takes two params, first params is 'R.equals'. It tells what should do with the second param. Here is checking whether they are equal.

Second param is an array, take tow expersions.

R.head --> xs[]
biggestNumberOfArray --> xs.sort((a,b) => b-a)[]

More example:

var average = R.converge(R.divide, [R.sum, R.length])
average([, , , , , , ]) //=> 4 var strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
strangeConcat("Yodel") //=> "YODELyodel"

[Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge的更多相关文章

  1. JS Function Arguments

    Function arguments在ECMAScript中的行为并不像其他大多数语言中的函数参数. 在ECMAScript中,function 并不关心有多少个参数传入函数中,也不关心传入参数的数据 ...

  2. [Javascript] Required function arguments in Javascript

    In Javascript, all function arguments are optional by default. That means if you ever forget to pass ...

  3. how to tell a function arguments length in js

    how to tell a function arguments length in js JavaScript函数不对参数值(参数)执行任何检查 https://www.w3schools.com/ ...

  4. js function arguments types

    js function arguments types https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functi ...

  5. JavaScript Function arguments.callee caller length return

    一.Function 函数是对象,函数名是指针. 函数名实际上是一个指向函数对象的指针. 使用不带圆括号的函数名是访问函数指针,并非调用函数. 函数的名字仅仅是一个包含指针的变量而已.即使在不同的环境 ...

  6. JavaScript Function.arguments 属性详解

    语法 [functionObject.]arguments arguments属性是正在执行的函数的内置属性,返回该函数的arguments对象.arguments对象包含了调用该函数时所传入的实际参 ...

  7. Parse the main function arguments

    int main(int argc, char **argv) { std::string reportDir; std::string transURL; std::string visualEle ...

  8. [PureScript] Introduce to PureScript Specify Function Arguments

    JavaScript does its error-checking at runtime, but PureScript has a compiler, which makes sure that ...

  9. pytest4-单文件使用fixture(Fixtures as Function arguments)

    Fixtures as Function arguments (fixture作为函数参数传入)Test functions can receive fixture objects by naming ...

随机推荐

  1. ASP.Net MVC Filter验证用户登录

    一.Filter是什么 ASP.NetMVC模式自带的过滤器Filter,是一种声明式编程方式,支持四种过滤器类型,各自是:Authorization(授权),Action(行为),Result(结果 ...

  2. shell脚本中的反引号,单引号,双引号与反斜杠

    转自:http://blog.sina.com.cn/s/blog_6561ca8c0102we2i.html 反引号位 (`)经常被忽略,而且容易与单引号弄混.它位于键盘的Tab键的上方.1键的左方 ...

  3. xpath使用方法详解id 、starts-with、contains、text()和last() 的用法

    1.XPATH使用方法 使用XPATH有如下几种方法定位元素(相比CSS选择器,方法稍微多一点): a.通过绝对路径定位元素(不推荐!) WebElement ele = driver.findEle ...

  4. HSV颜色模型

    HSV(Hue, Saturation, Value)是根据颜色的直观特性由A. R. Smith在1978年创建的一种颜色空间, 也称六角锥体模型(Hexcone Model). 注意的是OpenC ...

  5. 【习题 6-10 UVA - 246】10-20-30

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 发牌的那个牌堆用一个deque,7个牌堆用vector来模拟. 然后按照题意模拟就好. 不难. [代码] /* 1.Shoud it ...

  6. 【河南省多校脸萌第六场 A】分班级

    [链接]点击打开链接 [题意] 在这里写题意 [题解] 最大的给了最小的,实际上就对应了,最大值减1,最小值加1. 那么二分最后班级人数最小的最大可能是几->temp1; 二分最后班级人数最大的 ...

  7. SoapUI、Jmeter、Postman三种接口测试工具的比较分析——灰蓝

    前段时间忙于接口测试,也看了几款接口测试工具,简单从几个角度做了个比较,拿出来与诸位分享一下吧.各位如果要转载,请一定注明来源,最好在评论中告知博主一声,感谢.本报告从多个方面对接口测试的三款常用工具 ...

  8. ZOJ 1242 Carbon Dating

    UVA昨天上不去,今天一大早起来还是上不去 0.0 于是去ZOJ 这题大意就是半衰期... 取对数用到了换底公式...我都忘了这玩意了T T 上代码... #include<iostream&g ...

  9. amazeui学习笔记--js插件(UI增强4)--下拉组件Dropdown

    amazeui学习笔记--js插件(UI增强4)--下拉组件Dropdown 一.总结 1.am-dropdown(及其孩子):控制下拉列表的样式 2.data-am-dropdown(及其孩子):控 ...

  10. 【POJ 1226】Substrings

    [链接]h在这里写链接 [题意] 给你n个字符串. 让你找一个字符串s. 设s'为这个字符串的逆序. 要求s或者s'在每个字符串里都能够找得到. 并且要求s的长度最长. 求出这个最长的串的长度. [题 ...