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-14 UVA-1602】Lattice Animals

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 借鉴网上的题解的. 思路是. 用"标准化"的思想. 确定基准点(0,0) 然后假设(0,0)是第一个连通块. 然 ...

  2. Google 免费公共 DNS 服务器

    Google 免费公共 DNS 服务器 http://googleblog.blogspot.com/2009/12/introducing-google-public-dns.html DNS 8. ...

  3. JS实现拖拽小案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. [C/C++]_[0基础]_[static_cast,reinterpret_cast,dynimic_cast的使用场景和差别]

    场景: 1. C++的对象差别于C的原因是他们能够有继承关系, 方法有重载, 覆盖关系等, 他们的对象内存数据结构因此也比較复杂. 2. 非常多情况下我们须要一个父类来存储子类的指针对象进行通用方法的 ...

  5. Android Material风格的应用(三)--DrawerLayout

    添加抽屉导航 Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndroid Mater ...

  6. 推荐一款稳定快速免费的前端开源项目 CDN 加速服务

    前面学习到什么是CDN,全称是Content Delivery Network,即内容分发网络.CDN的通俗理解就是网站加速,CPU均衡负载. CDN的基本思路是尽可能避开互联网上有可能影响数据传输速 ...

  7. (转载)iis7下站点日志默认位置

    转自http://www.cnblogs.com/mincyw/p/3425468.html iis7下站点日志默认位置   在iis6时,通过iis管理器的日志配置可以找到站点日志存储的位置. 但是 ...

  8. OC学习篇之---第一个程序HelloWorld

    从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这里最好还是Java).在干活之前,得先找到 ...

  9. 【例题5-6 UVA 540 】Team Queue

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用两个队列模拟就好. 记录某个队在不在队列里面. 模拟 [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] #in ...

  10. License控制实现原理(20140808)

    近期须要做一个License控制的实现,做了一个设计,设计图例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVjX2Zlbmc=/font/5 ...