[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 ...
随机推荐
- BZOJ.2125.最短路(仙人掌 圆方树)
题目链接 圆方树.做题思路不写了.. 就是当LCA是方点时跳进那个环可以分类讨论一下用树剖而不必须用倍增: 如果v是u的(唯一的那个)重儿子,那么u的DFS序上+1的点即是要找的:否则v会引出一条新的 ...
- [POI2012]Salaries
题目大意: 给定一棵n带权树,每个点的权值在[1,n]范围内且互不相等,并满足子结点的权值一定小于父结点. 现在已知一个包含根结点的联通块中个点的权值,求剩下哪些点的权值能够被求出,并求出这些权值. ...
- Python168的学习笔记1
在对list的条件选择有两种常用方法,直接使用filter函数,就是filter(func,sequence);另外一种就是迭代操作,类似 x for x in sequence func.这两种方法 ...
- cookie、sessionStorage、localStorage 详解
转自--http://www.cnblogs.com/fly_dragon/p/3946012.html cookie Cookie的大小.格式.存储数据格式等限制,网站应用如果想在浏览器端存储用户的 ...
- SQL2008″Unable to read the list of previously registered servers on this system”
打开SQL2008,弹出”Unable to read the list of previously registered servers on this system”错误, 微软官方的解决方法:h ...
- Windows Server 2016 Essentials试用
下载地址: Windows Server 2016 Essentials (x64) SHA-1: 6E1D1880873157ADCEF3D74363308A95DC89103D ed ...
- IntelliJ IDEA使用教程三 SVN的集成与使用
注意: 虽然IDEA已经集成了svn客户端,但还是习惯使用第三方svn客户端,比如: TortoiseSVN. 就是因为使用的是第三方客户端,所以和IDEA集成的时候就出现了一个特别大的坑,因为svn ...
- 关于ANDROID模拟器的一些事
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 继上一篇Android Studio VS Eclipse的文章后接着来分享AnDevCo ...
- C#程序集系列12,C#编译器和CLR如何找寻程序集
本篇体验C#编译器和CLR运行时如何查找程序集,并自定义CLR运行时查找程序集的方式. □ C#编译器和CLR运行时如何查找程序集 C#编译器在哪里?--在C:\Windows\Microsoft.N ...
- https跳转到http的过程在IE6中存在BUG(Bea-090475)
前段时间做OA系统的https的安全登录功能(以前登录是采用的一般的http方式,后因为安全性考虑需要改成https的方式)在本机测试完全通过. 可是近期同事发现在测试环境下用IE6访问会出现不能访问 ...