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. 【例7-15 UVA-1603】Square Destroyer

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出所有的正方形(长度为1,2...n的)所包含哪些边. 然后记录每个正方形的应有边长和实际边长(有些边被删掉了); 然后搜的 ...

  2. perl模块 Compress::Raw::Lzma 的安装

    perl模块 Compress::Raw::Lzma 的安装 用 cpan 安装任意perl模块总是提示 Couldn't untar Compress-Raw-Lzma-2.070.tar: 'Ca ...

  3. string-format样式使用

    首先我们看如下代码 protected String calcu1() { StringBuffer resultB = new StringBuffer(); String str = null; ...

  4. Oracle 中的Interger类型

    引自 wolfAone, oracle有没有integer类型,这种类型的最大值是多少啊. Integer是Number类型的子类型: NUMBER Type You use the NUMBER d ...

  5. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  6. java測试网络连接是否成功并设置超时时间

    /** * 获取RMI接口状态 * * @return "0":服务正常,"1": 连接报错,"2":连接超时 */ @Override p ...

  7. Java中字节与对象之间的转换

    近期公司里面用到了消息队列,而正如我们知道的是消息队列之间的是通过二进制形式的.以下就分享一下java中字节与对象之间的转换. 主要是用到了ByteArrayOutputStream和ObjectOu ...

  8. android闹钟实现原理

    闹钟的原理可用下面我自己画的一幅图来概括:(不对的地方,尽管吐槽) 我们来看看新建闹钟到闹钟响铃的步骤:    1.新建一个闹钟: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  9. 【BZOJ 3675】[Apio2014]序列分割

    [链接] 链接 [题意] 在这里输入题意 [题解] 模拟一下样例. 会发现.切的顺序不影响最后的答案. 只要切点确定了. 答案就确定了. 则设f[i][j]表示前i段,第i段保留到j的最大值. \(f ...

  10. 【u250】manhattan

    Time Limit: 1 second Memory Limit: 64 MB [问题描述] 混乱的城市已经变得无法控制.大楼随处乱造,城市的布局也是一片混乱.市长决定要结束这种局面,兵器并且想建造 ...