[Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
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的更多相关文章
- JS Function Arguments
Function arguments在ECMAScript中的行为并不像其他大多数语言中的函数参数. 在ECMAScript中,function 并不关心有多少个参数传入函数中,也不关心传入参数的数据 ...
- [Javascript] Required function arguments in Javascript
In Javascript, all function arguments are optional by default. That means if you ever forget to pass ...
- how to tell a function arguments length in js
how to tell a function arguments length in js JavaScript函数不对参数值(参数)执行任何检查 https://www.w3schools.com/ ...
- js function arguments types
js function arguments types https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functi ...
- JavaScript Function arguments.callee caller length return
一.Function 函数是对象,函数名是指针. 函数名实际上是一个指向函数对象的指针. 使用不带圆括号的函数名是访问函数指针,并非调用函数. 函数的名字仅仅是一个包含指针的变量而已.即使在不同的环境 ...
- JavaScript Function.arguments 属性详解
语法 [functionObject.]arguments arguments属性是正在执行的函数的内置属性,返回该函数的arguments对象.arguments对象包含了调用该函数时所传入的实际参 ...
- Parse the main function arguments
int main(int argc, char **argv) { std::string reportDir; std::string transURL; std::string visualEle ...
- [PureScript] Introduce to PureScript Specify Function Arguments
JavaScript does its error-checking at runtime, but PureScript has a compiler, which makes sure that ...
- pytest4-单文件使用fixture(Fixtures as Function arguments)
Fixtures as Function arguments (fixture作为函数参数传入)Test functions can receive fixture objects by naming ...
随机推荐
- Delphi部份函数,命令,属性中文说明
Abort 函数 引起放弃的意外处理 Abs 函数 绝对值函数 AddExitProc 函数 将一过程添加到运行时库的结束过程表中 Addr 函数 返回指定对象的地址 AdjustLineBreaks ...
- 5lession-path路径相关操作
今天开始接触到了文件目录.路径方面的知识点.记录如下 先看代码 #!/usr/bin/python # -*- coding: utf-8 -*- import os import sys curre ...
- rhel5安装 oracle10
readhat 安装11gr2文档 需要注意的地方:必须关掉的 1,防火墙:2,SElinux . root 用户运行 setup 命令可关防火墙与SElinux 修改网络配置文件,一定要重启此文 ...
- document.write的注意点
如果给button点击事件添加document.write会消除页面所有元素,包括button按钮 <!DOCTYPE html> <html> <head> &l ...
- Flask项目之手机端租房网站的实战开发(六)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- Java基础学习总结(52)——Liunx系统Centos上搭建Java开发环境
一.安装jdk 1.查看Linux自带的JDK是否已安装 [plain] view plain copy print? java –version 如果出现openjdk,最好还是先卸载掉openjd ...
- JS学习笔记 - 面向对象
类.对象类:模子对象:产品(成品) 蛋糕(对象) 模子(类) Array 类 arr 对象 Array.push(); 错 arr.push(); 对 new arr(); 错 原型prototype ...
- 洛谷—— P1018 乘积最大
https://www.luogu.org/problem/show?pid=1018#sub 题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年 ...
- (6)uboot具体解释——关闭缓存和mmu
uboot具体解释--关闭缓存和mmu 当设置完时钟分频以后,uboot就会运行cpu_init_crit汇编函数,这个函数的主要作用就是关闭缓存和mmu.然后调用lowlevel_init函数进行系 ...
- ORACLE中的Net Configuration Assistant 点击后无反应, sqlplus登录数据库提示Oracle11g ORA-12560: TNS: 协议适配器错误
首先是对于点击无反应问题: 如果是客户端下的Net Configuration Assistant可用,而服务器端的Net Configuration Assistant等工具不可用的原因如下. 环境 ...