[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 ...
随机推荐
- CF1060E Sergey and Subway 思维
分两种情况讨论 一种为奇数长为$L$的路径,在经过变化后,我们需要走$\frac{L}{2} + 1$步 一种为偶数长为$L$的路径,在变化后,我们需要走$\frac{L}{2}$步 那么,我们只需要 ...
- [BZOJ4883][Lydsy1705月赛]棋盘上的守卫(Kruskal)
对每行每列分别建一个点,问题转为选n+m条边,并给每条边选一个点覆盖,使每个点都被覆盖.也就是最小生成环套树森林. 用和Kruskal一样的方法,将边从小到大排序,若一条边被选入后连通块仍然是一个环套 ...
- BZOJ 4826: [Hnoi2017]影魔 单调栈 主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=4826 年少不知空间贵,相顾mle空流泪. 和上一道主席树求的东西差不多,求两种对 1. max(a ...
- BZOJ.2527.[POI2011]MET-Meteors(整体二分)
题目链接 BZOJ 洛谷 每个国家的答案可以二分+求前缀和,于是可以想到整体二分. 在每次Solve()中要更新所有国家得到的值,不同位置的空间站对应不同国家比较麻烦. 注意到每次Solve()其国家 ...
- MyBatis -- generator 逆向工程
一.引言 官网文档:http://www.mybatis.org/generator/index.html 通过使用官方提供的mapper自动生成工具,mybatis-generator-core-1 ...
- HAproxy和TIME WAIT的一次问题排查
近日平稳运行了将近4年的发号器突然出现问题,在元旦0分的时候出现短暂的性能下降,导致发号失败率飙高到一个不可接收的值,哎,意外总是发生在你想不到的地方. 这几天赶紧和小伙伴们赶紧追查原因,制定改造方案 ...
- UEFI引导模式
Author: JinDate: 20140827System: windows 刚帮楼下的公司解决了个问题. 原来的办公电脑,预装linux,他们重装成win7.新买的电脑预装成win8,安装出问题 ...
- Tasker to auto record incoming or outgoing call
Tasker to auto record incoming or outgoing call most of time, i was thinking of tasker can do the jo ...
- 如何设置Android手机的sqlite3命令环境
1.在PC环境下启动一个模拟器(不是手机) 2.查看模拟器 /systen/xbin是否有sqlite3命令 adb shell cd /system/xbin ls 3.把模拟器 /system/x ...
- oracle sql语句怎么查询所有存储过程中是否包含某个注释?
select text from all_source where type='PROCEDDURE' and name='过程名'and instr(text,'注释内容')>0