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的更多相关文章

  1. JavaScript基础Curry化(021)

    时候我们希望函数可以分步接受参数,并在所有参数都到位后得到执行结果.为了实现这种机制,我们先了解函数在Javascript中的应用过程: 1. 函数的“应用”(Function Application ...

  2. [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 ...

  3. [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. ...

  4. 初涉JavaScript模式 (10) : 函数 【进阶用法】

    写在前面 不知不觉写到第10篇了.这篇写起来很忐忑,终于和高级搭上边了(呵呵),这篇我们 主要 说一下 JS 方法的部分高级用法(我知道的),笔者水平有限,难免有错.废话不多少,进入正文. 初始化 我 ...

  5. JavaScript-Curry

    Currying allows you to easily create custom functions by partially invoking an existing function. He ...

  6. js函数式编程(二)-柯里化

    这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...

  7. JS高级技巧学习小结

    安全类型检測 var isArray = value instanceof Array; 以上代码要返回true,value必须是一个数组,并且还必须与Array构造函数在同一个全局作用域中(Arra ...

  8. Currying 及应用

    Currying,中文多翻译为柯里化,感觉这个音译还没有达到类似 Humor 之于幽默的传神地步,后面直接使用 Currying. 什么是 Currying Currying 是这么一种机制,它将一个 ...

  9. JavaScript Patterns 4.10 Curry

    Function Application apply() takes two parameters: the first one is an object to bind to this inside ...

随机推荐

  1. type="submit"表单提交理解

    1.默认为form提交表单 . button则响应用户自定义的事件,如果不指定onclick等事件处理函数,它是不做任何事情.当然,button也可以完成表单提交的工作. 2.method=" ...

  2. JavaSE基础之矩阵运算

    JavaSE基础之矩阵运算 1.矩阵类:Matrix.java 包括矩阵的加.乘运算,行列式的求解,最大最小元素等 package cn.com.zfc.help; import java.text. ...

  3. HTTP 499 状态码 nginx下 499错误[转]

    日志记录中HTTP状态码出现499错误有多种情况,我遇到的一种情况是nginx反代到一个永远打不开的后端,就这样了,日志状态记录是499.发送字节数是0. 老是有用户反映网站系统时好时坏,因为线上的产 ...

  4. hdu 2110 基础母函数

    题意:退出本身并不麻烦,麻烦的是,退出的人需要取走相应比例(1/3)金额的资产.假设公司此时一共有n种价值的资产,每种价值的资产数量已知,请帮助心烦意乱的XHD夫妇计算一共有多少种分割资产的方法.   ...

  5. python开发_tkinter_修改tkinter窗口的红色图标'Tk'

    学过java的swing可能知道,在创建一个窗口的时候,窗口的左上角是一个咖啡图标 如下图所示: 在python中,tkinter模块生成的窗口左上角是一个:Tk字样的图标(Tk为tkinter的缩写 ...

  6. UVALive 5971

    Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...

  7. LAMP LNMP 和 LNMPA

    LAMP指的是:Linux+Apache+MySQL+Perl/PHP/Python LAMP是一个缩写,它指一组通常一起使用来运行动态网站或者服务器的自由软件: Linux,操作系统: Apache ...

  8. unix环境高级编程----进程控制wait()

    一.wait()函数 当一个进程中调用wait()函数的时候 (1)假设其全部的子程序都还在执行,则堵塞 (2)假设一个子进程已终止.则等待父进程获取其终止状态. (3)假设没有子进程,则返回错误. ...

  9. Oracle 11g 错误:ORA-28002: the password will expire within 7 days 解决方法

    ERROR:ORA-28002: the password will expire within 7 days 错误是提示password快过期了,有两个办法解决问题. 一. 改动已经报错用户的pas ...

  10. C#中List<T>是怎么存放元素的

    Jeffrey Zhao在"你的字典里有多少元素?"一文中,提到了他在面试时问过的一个问题:List<T>是怎么存放元素?不幸的是,自己也回答不出来,只知道怎么用,却不 ...