[Javascript] Understand Curry
The act of currying can be described as taking a multivariate function and turning it into a series of unary functions.
Let's see an example:
const add = a => b=> a + b;
const inc = add(1); const res = inc(3) //
This is a very basic currying example.
We wrote a function 'add' take a param 'a' return a function which take param 'b', finally return a + b;
We can based on 'add' function to create another function 'inc'. This approach is good for resuability. But there is one problem, because you cannot do:
add(1,3)
To solve the problem we need to write a 'curry' function, the basic idea is:
1. Check the passed in function, how many params it should takes, in our case 'add' function take 2 params.
2. 'curry' should return another function, inside function it should check, if length of params is the same as function's params, in our case is:
add(1,3)
Then we invoke function immediately. (Using .call for immediately invoke)
3. Otherwise, we should still return a function with param partially applyied. (using .bind for partially apply)
function curry(fn) {
// The number of fn's params
// fn = (a, b) => a + b
// then length is 2
const arity = fn.length;
console.log(arity)
return function $curry(...args) {
console.log(args, args.length)
if (args.length < arity) {
// partially apply
// in case of add(1)(2)
return $curry.bind(null, ...args);
}
// immediately invoke
// in case of add(1,2)
return fn.call(null, ...args);
};
};
const add = curry((a, b) => a + b);
const inc = add(1);
const res = inc(2) //
Arity describes the number of arguments a function receives. Depending on the number it receives, there are specific words to describe these functions.
A function that receives one is called a unary function.
A function that receives two arguments is called a binary,
three equals a ternary,
and four equals a quaternary,
so forth and so on.
[Javascript] Understand Curry的更多相关文章
- JavaScript基础Curry化(021)
时候我们希望函数可以分步接受参数,并在所有参数都到位后得到执行结果.为了实现这种机制,我们先了解函数在Javascript中的应用过程: 1. 函数的“应用”(Function Application ...
- [Javascript] Understand common misconceptions about ES6's const keyword
Values assigned with let and const are seen everywhere in JavaScript. It's become common to hear the ...
- [Javascript] Understand Function Composition By Building Compose and ComposeAll Utility Functions
Function composition allows us to build up powerful functions from smaller, more focused functions. ...
- 初涉JavaScript模式 (10) : 函数 【进阶用法】
写在前面 不知不觉写到第10篇了.这篇写起来很忐忑,终于和高级搭上边了(呵呵),这篇我们 主要 说一下 JS 方法的部分高级用法(我知道的),笔者水平有限,难免有错.废话不多少,进入正文. 初始化 我 ...
- JavaScript-Curry
Currying allows you to easily create custom functions by partially invoking an existing function. He ...
- js函数式编程(二)-柯里化
这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...
- JS高级技巧学习小结
安全类型检測 var isArray = value instanceof Array; 以上代码要返回true,value必须是一个数组,并且还必须与Array构造函数在同一个全局作用域中(Arra ...
- Currying 及应用
Currying,中文多翻译为柯里化,感觉这个音译还没有达到类似 Humor 之于幽默的传神地步,后面直接使用 Currying. 什么是 Currying Currying 是这么一种机制,它将一个 ...
- JavaScript Patterns 4.10 Curry
Function Application apply() takes two parameters: the first one is an object to bind to this inside ...
随机推荐
- gzez某蒟蒻lyy的博客
在gz,想去sn幻想乡也行,现在高一并且是已经高二但仍然是机房最弱,没救了 愿诸位身体健康 水平不行,写出来的东西很sb,但还是会偶尔记录一下... 数学公式测试:$\binom n{n_1\cdot ...
- 【二分答案】Google Code Jam Round 1A 2018
题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一 ...
- 重庆市队选拔 CQOI2015 解题报告
文章链接:http://www.cnblogs.com/Asm-Definer/p/4434601.html 题目链接:http://pan.baidu.com/s/1mgxIKli 官方数据:htt ...
- WIKIOI 1026 逃跑的拉尔夫 深度优先搜索
/* 1026 逃跑的拉尔夫 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆 ...
- wikioi 1576 最长严格上升子序列
简单的最长严格上升子序列的题 dp[i]表示到a[i]这个数为最后的时候最大的长度是多少 然后就差不多了吧~ #include <cstdio> #include <cmath> ...
- ROS知识(8)----CMakeLists.txt文件编写的理解
ROS(Indigo)编程必须要理解CMakeList.txt的编写规则,教程地址:catkin/CMakeLists.txt,官网有相关的教程,中文的翻译版本写的很不错,教程地址:ROS中的CMak ...
- CentOS6安装redmine
Author: JinDate: 20140827System: CentOS release 6.5 (Final) 参考:http://www.redmine.org/projects/redmi ...
- 2007 Audi A4 INSTRUMENT CLUSTER WIRING DIAGRAM
BOSCH RB8 8E0920 951G I found the answer by myself...... Here is what it's work for me. GREEN CONNEC ...
- ARM FPGA Extended Memory Interface
Connect a ARM Microcontroller to a FPGA using its Extended Memory Interface (EMI) http://elinux.org/ ...
- xapian的使用
1.先来看一下Xapian的介绍: Xapian的官方网站是http://www.xapian.org,这是一个非常优秀的开源搜索引擎项目,搜索引擎其实只是一个通俗的说法,正式的说法其实是IR(Inf ...