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. 2 Spring4 之Bean的配置

    Spring4 之Bean的配置 1 IOC & DI 概述 IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源 ...

  2. lnmp环境一键搭建及卸载

    系统需求: CentOS/Debian/Ubuntu Linux系统 需要2GB以上硬盘剩余空间 128M以上内存,OpenVZ的建议192MB以上(小内存请勿使用64位系统) VPS或服务器必须已经 ...

  3. 浙江省队选拔 ZJOI2015 (Round 1) 解题报告

    最近莫名其妙地喜欢上了用这种格式写各省省选的全套题解= = 今年浙江省选的出题人是算法竞赛界传说级人物陈立杰,看样子他的出题风格很有特点……ABC三题难度是严格递减的,感觉如果在做第一题的时候被卡住的 ...

  4. BZOJ 2754 SCOI 2012 喵星球上的点名 后缀数组 树状数组

    2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2068  Solved: 907[Submit][St ...

  5. pygame系列_pygame的各模块叙述

    在pygame中,有很多模块,每个模块对应着不同的功能,如果我们知道这些模块是做什么的,那么,对我们的游戏开发会起到关键性的作用. 我们就说说pygame中的各个模块吧!!! #pygame modu ...

  6. TYVJ 1463 智商问题 分块

    TYVJ 1463 智商问题 Time Limit: 1.5 Sec  Memory Limit: 512 MB 题目连接 http://www.tyvj.cn/p/1463 背景 各种数据结构帝~ ...

  7. RN生命周期

    网上看的博客,看着写的很好,想深入学RN的详细看下之后,再自己敲敲吧!有助于身体健康! 一个RN组件从它被加载,到最终被卸载会经历一个完整的生命周期.所谓生命周期,就是一个对象从开始生成到最后消亡所经 ...

  8. PostgreSQL各命令行工具功能说明

    I. SQL 命令 II. PostgreSQL 客户端应用 clusterdb -- 聚簇一个PostgreSQL数据库 createdb -- 创建一个新的PostgreSQL数据库 create ...

  9. react-native开发总结

    项目地址:http://liu12fei08fei.github.io/blog/41react-native.html 说明 • 项目总结代码地址 • 从项目开始启动(2018-07-02)到项目进 ...

  10. 与Win8之磁盘活动时间100%斗争心得

    Windows8因人而异地会在使用过程中磁盘活动时间无缘无故提升到100%并且可能出现持续性抽风现象,具体表现为0%瞬间飙升至100%后又回落,或者一直保持在100%导致使用过程卡顿,认真阅读本文有助 ...