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:

  1. const shouldBeTrue = [, , , , , ]
  2. const shouldBeFalse = [, , , , ]
  3.  
  4. const isFirstBiggest = (xs) => xs[] == xs.sort((a, b) => b - a)[]
  5.  
  6. console.log(isFirstBiggest(shouldBeTrue)) // true
  7. console.log(isFirstBiggest(shouldBeFalse)) // false

In the code we can see this:

  1. 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:

  1. const shouldBeTrue = [, , , , , ]
  2. const shouldBeFalse = [, , , , ]
  3.  
  4. import {
  5. converge, equals, head, sort, descend, identity, compose
  6. }
  7. from 'ramda'
  8.  
  9. const biggestNumberOfArray = compose(
  10. head,
  11. sort(descend(identity))
  12. );
  13. const isFirstBiggest = converge(
  14. equals, [
  15. head,
  16. biggestNumberOfArray
  17. ]
  18. );
  19.  
  20. // xs =>
  21. // xs[0] == xs.sort((a, b) => b - a)[0]
  22.  
  23. console.log(isFirstBiggest(shouldBeTrue))
  24. console.log(isFirstBiggest(shouldBeFalse))

So in the code:

  1. converge(
  2. equals, [
  3. head,
  4. biggestNumberOfArray
  5. ]
  6. )

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.

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

More example:

  1. var average = R.converge(R.divide, [R.sum, R.length])
  2. average([, , , , , , ]) //=> 4
  3.  
  4. var strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
  5. 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. 使用IPV6

    使用IPV6 知道IPV6已经很久了,但是一直没有使用过. 我使用的IPV4网络一般是 内网-->外网-->互联网,IPV6也不外乎这样,但是对IPV6而言,必须有它的"世界&q ...

  2. Impala基础认知与安装

    一.Impala简介 Cloudera Impala对你存储在Apache Hadoop在HDFS,HBase的数据提供直接查询互动的SQL.除了像Hive使用相同的统一存储平台,Impala也使用相 ...

  3. VC 常见问题百问

    http://www.cnblogs.com/cy163/archive/2006/06/19/429796.html 经典Vc书 Charles Petzold 的<Programming W ...

  4. [ACM] POJ 1046 Color Me Less

    Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30146   Accepted: 14634 D ...

  5. set_fix_multiple_port_nets

    set_fix_multiple_port_nets   -all    -buffer_constants 加上这个命令之后 综合之后的网表就不会出现assign语句 否则会出现

  6. CSS3实现的立体button

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 如何启用“锁定内存页”选项 (Windows)

    默认情况下,禁用 Windows 策略"锁定内存页"选项.必须启用此权限才能配置地址窗口化扩展插件 (AWE).此策略将确定哪些帐户可以使用进程将数据保留在物理内存中,从而阻止系统 ...

  8. 8.5 Android灯光系统_源码分析_通知灯

    参考文章(应用程序举例)how to use the LED with Android phonehttp://androidblogger.blogspot.jp/2009/09/tutorial- ...

  9. 【SPOJ 694】Distinct Substrings

    [链接]h在这里写链接 [题意]     给你一个长度最多为1000的字符串     让你求出一个数x,这个x=这个字符串的不同子串个数; [题解]     后缀数组题.     把原串复制一份,加在 ...

  10. 数据结构与算法实验题 4.2 Who is the strongest

    数据结构与算法实验题 4.2 Who is the strongest ★实验任务 在神奇的魔法世界,召唤师召唤了一群的魁偶.这些魁偶排成一排,每个魁偶都有一个 战斗值.现在该召唤师有一个技能,该技能 ...