[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 ...
随机推荐
- 02-c#基础之01-基础语法(三)
1.赋值运算符:"=" =:表示赋值的意思,表示把等号右边的值,赋值给等号左边的变量. 由等号连接的表达式称之为赋值表达式. 注意:每个表达式我们都可以求解除一个定值,对于赋值表达 ...
- [POI2015]Logistyka
[POI2015]Logistyka 题目大意: 一个长度为\(n(n\le10^6)\)的数列\(A_i\),初始全为\(0\).操作共\(m(m\le10^6)\)次,包含以下两种: 将\(A_x ...
- Shell 学习笔记之传递参数
传递参数 设置权限 chmod +x file.sh 传递参数 ./file.sh parameter1 ... 特殊字符 $# 传递到脚本的参数个数 $* 以一个单字符串的形式显示所有向脚本传递的参 ...
- mac os 切换网络优先级
升级到新系统OS X Yesemite 系统后有WIFI时会默认使用WIFI而不是有线. 但是公司的WIFI基本没法用,每次到公司之后就得把WIFI关掉,回家又打开,烦死了. 今天研究了下原来网络优先 ...
- codevs 1191 树轴染色 线段树区间定值,求和
codevs 1191 树轴染色 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.codevs.cn/problem/1191/ Des ...
- js跨域请求提示函数未定义的问题
我的代码是这么写的 window.onload=function(){ function sendRequest(){ var script=document.getElementById(" ...
- Shell脚本里的双冒号是什么意思
这个是代码开发风格,其实也就是一个函数名,相当于下划线分割,但改读成包名之后就意义不一样.这个是根据Google的Shell开发规范进行定义的. 参考: https://google.github.i ...
- Tasker : Task / Shortcut Widgets
Task / Shortcut Widgets The standard way of running a Tasker task is by attaching it to a profile wh ...
- maven进阶:一个多模块项目
一个多模块项目通过一个父POM 引用一个或多个子模块来定义.父项目,通过以下配置,将子项目关联. <packaging>pom</packaging> <modules& ...
- Big Number------HDOJ杭电1212(大数运算)
Problem Description As we know, Big Number is always troublesome. But it's really important in our A ...