当前位置:网站首页>js currying
js currying
2022-07-30 09:08:00 【Li Jianyi】
柯里化
概念
柯里化(currying)又称部分求值,is a technique for converting a multi-parameter function to a one-parameter function.
Currying is the way to go f(a, b, c) The functional form of is changed to f(a)(b)的形式.柯里化不会调用函数,Just transform the function.
原理
A curried function of a function,is to pass in a function as a parameter“柯里化函数”,The function receives the incoming function parameters inside the function,After receiving the function does not evaluate,而是继续返回另一个函数,Save the parameters just passed in as a closure,After all parameters have been received,trigger evaluation.
效果
/** * 柯里化函数 * @param {function} f 需要柯里化的函数 * */
function curry(f) {
return fucntion(a) {
return function(b) {
return function(c) {
f(a, b, c);
}
}
}
};
function sum(a, b, c) {
return a + b + c;
}
let currySum = curry(sum);
console.log(currySum(1, 2, 3));
高级柯里化
function curry(f) {
return function curried(...args) {
if(args.length >= f.length) {
return f.apply(this, args);
}else {
return function(...args1) {
return curried.apply(this, args.concat(args2));
}
}
}
}
// 使用方式
function sum(a, b, c) {
return a + b + c;
}
let currySum = curry(sum);
console.log(currySum(1, 2, 3));
console.log(currySum(1, 2)(3));
console.log(currySum(1)(2)(3));
边栏推荐
- C语言力扣第46题之全排列。回溯法
- 电脑文档误删除怎么恢复,恢复误删除电脑文档的方法
- [GAN]老照片修复Bringing Old Photos Back to Life论文总结
- ES: 箭头函数和包裹它的代码共享相同的this
- 万字详解:C语言三子棋进阶 + N子棋递归动态判断输赢(另附课设大作业参考)
- 【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作
- 基于SSM开发实现校园疫情防控管理系统
- 注解开发相关
- ES报错处理-mapper [xx.xx] of different type, current_type [text], merged_type [keyword]
- 孙洪鹤讲教材:原点+众筹+产品,逆向营销实战操作方案
猜你喜欢

sql注入数据库原理详解

K-Net:Towards Unified Image Segmentation,基于动态内核的通用分割网络,(NMS-free and Box-free),从语义/实例分割到全景分割。

开创ETC生态建设新格局 JASMINER新一批X4服务器陆续发出

电脑文档误删除怎么恢复,恢复误删除电脑文档的方法

20个电路能懂5个以上,足以证明你在电子行业混过!

40.【vector的运用】

【Kotlin 中的类和继承】

手把手教学OneOS FOTA升级

golang grpc protoc 环境配置

SwiftUI SQLite 教程之 构建App本地数据库实现创建、读取、更新和删除(教程含完成项目源码)
随机推荐
JS中如何阻止事件冒泡和默认行为
Selected as one of the "Top Ten Hard Core Technologies", explaining the technical points of Trusted Confidential Computing (TECC) in detail
hcip实验
【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作
41.【vector应用操作2】
ES:解构
npm指令
typescript2-typescript为什么给js添加类型支持
opencv可以有多有趣
ES:class 的基本使用
typescript1-typescript是什么
SQL注入漏洞(postgresql注入)
typescript8 - type annotations
【无标题】
input标签的tabindex属性 & a标签的tabindex属性
AutoSAR EcuM系列01- EcuM模块的功能概述和变体类型
MagicDraw secondary development process
求大佬解答,这种 sql 应该怎么写?
RFID固定资产盘点系统给企业带来哪些便利?
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs