[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 ...
随机推荐
- 从数据库中生成XML文件
前台页面<%@ page language="java" contentType="text/html; charset=utf-8" pageEncod ...
- web.xml的配置及加载顺序
一web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> ...
- 上拉刷新,下拉加载(JQuery)
<script type="text/javascript"> $(document).ready(function() { $(w ...
- Java Web学习总结(5)——HttpServletResponse对象详解
一.HttpServletResponse常见应用--生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类, 生成随机图片范例: package gacl.r ...
- iOS 之应用性能调优的25个建议和技巧
注意:每在优化代码之前,你都要注意一个问题,不要养成"预优化"代码的错误习惯. 时常使用Instruments去profile你的代码来发现须要提升的方面.Matt Gallowa ...
- Codeforces 232A - Cycles (构造 + 思维)
题目链接: 232A - Cycles(点击打开) 题意: 要构成一个存在 \(k\) 个三元环的图,需要多少个点,输出顶点数 \(n\),并输出图. 题解: 题目中的任何图都可以用 \(90\)~ ...
- 8、for 、emumrate、range、if
1.for循环用户按照顺序循环可迭代对象中的内容,PS:break.continueli = [11,22,33,44]for item in li: print item 2.enumrate 为可 ...
- centos php 安装memcached 扩展 支持sasl
1.安装sasl yum install cyrus-sasl-lib.x86_64 yum install cyrus-sasl-devel.x86_64 2.下载libmemcached wget ...
- lettuce--Advanced Redis client
redis官方提供的java client: git地址:https://github.com/mp911de/lettuceAdvanced Redis client for thread-safe ...
- Eclipse 出错 Error:Could not create the Java Virtual Machine Error:A fatal exception has occurred
提示如下: scala compile server. error:could not create the java machine.Error: A fatal exception has occ ...