当前位置:网站首页>Function coritization
Function coritization
2022-07-06 08:29:00 【Wind billows】
currying
In mathematics and Computer Science , Currierization is a technique that transforms a function with multiple parameters into a series of functions with one parameter .
in other words , When a function has multiple parameters, it is called by passing some parameters ( These parameters will never change in the future ), Then it returns a new function to receive the remaining parameters , Return results , This is the currification of functions .
function checkAge(min){
return function(age){
return age >= nim;
}
}
effect
The main purpose of front-end using Coriolis is to simplify the code structure , Improve the maintainability of the system , One way , There is only one parameter , It enforces the unity of function , It's natural to achieve functional cohesion , To reduce the coupling . The advantage of physics and chemistry is to reduce the repetition of code , Improve the adaptability of codeCall mode
function add(a,b,c){
};
var newAdd = Curry(add); // After being processed by Coriolis function add function
newAdd(1)(2)(3)
newAdd(1,2)(3);
function curry (func) {
// Get the number of formal parameters of the function , Can pass Function name .length
// args Indicates the actual parameters passed in
// Take the famous function here , Not anonymous functions , Because when the number of arguments and formal parameters are the same , You need to call this function
return function curriedFn (...args) {
// ES6 grammar
// Determine the number of actual and formal parameters
if (args.length < func.length) {
// When the passed in parameter add The remaining parameters , Equal to the number of formal parameters , Execute the following code
return function () {
// When getting this call , Incoming parameter
return curriedFn(...args.concat(Array.from(arguments)))
}
}
// When the number of arguments is greater than or equal to the number of formal parameters , call func, Return results
return func(...args) // ES6 grammar ... Expand the array
}
}
// Functions to be coriolised
function getSum (a, b, c) {
return a + b + c
}
let curried = curry(getSum)
// test
// (args.length = 3) = func.length, Call directly func, Return results
curried(1, 2, 3)
// 1. (args.length = 1) < func.length , Executing anonymous functions , call curriedFn, Pass in the parameter 2, here args.length = 2
// 2. (args.length = 2) < func.length, Executing anonymous functions , call curriedFn, Pass in the parameter 3, here args.length = 3
// 3. (args.length = 3) = func.length, Call directly func, Return results
curried(1)(2)(3)
// 1. (args.length = 2) < func.length , Executing anonymous functions , call curriedFn, Pass in the parameter 3, here args.length = 3
// 2. (args.length = 3) = func.length, Call directly func, Return results
curried(1, 2)(3)
边栏推荐
- Analysis of pointer and array written test questions
- MySQL learning record 07 index (simple understanding)
- 华为云OBS文件上传下载工具类
- leetcode刷题 (5.31) 字符串
- Tidb backup and recovery introduction
- Migrate data from CSV files to tidb
- Huawei cloud OBS file upload and download tool class
- [research materials] 2021 live broadcast annual data report of e-commerce - Download attached
- On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
- Image fusion -- challenges, opportunities and Countermeasures
猜你喜欢
Synchronized solves problems caused by sharing
From monomer structure to microservice architecture, introduction to microservices
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
vulnhub hackme: 1
堆排序详解
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
[research materials] 2022 China yuancosmos white paper - Download attached
PLT in Matplotlib tight_ layout()
Pyqt5 development tips - obtain Manhattan distance between coordinates
leetcode刷题 (5.28) 哈希表
随机推荐
Bottom up - physical layer
All the ArrayList knowledge you want to know is here
sys. argv
[2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
图像融合--挑战、机遇与对策
1204 character deletion operation (2)
C语言 - 位段
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
【MySQL】数据库的存储过程与存储函数通关教程(完整版)
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
2022.02.13 - 238. Maximum number of "balloons"
PLT in Matplotlib tight_ layout()
Leetcode question brushing (5.28) hash table
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
C language custom type: struct
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
[2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
Ruffian Heng embedded bimonthly, issue 49