当前位置:网站首页>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)
边栏推荐
- China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
- Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
- Golang force buckle leetcode 1020 Number of enclaves
- [MySQL] lock
- NFT smart contract release, blind box, public offering technology practice -- contract
- Migrate data from a tidb cluster to another tidb cluster
- Leetcode question brushing (5.28) hash table
- Analysis of Top1 accuracy and top5 accuracy examples
- Deep learning: derivation of shallow neural networks and deep neural networks
- ESP系列引脚说明图汇总
猜你喜欢
NFT smart contract release, blind box, public offering technology practice -- contract
IOT -- interpreting the four tier architecture of the Internet of things
Leetcode question brushing (5.28) hash table
【MySQL】日志
MySQL learning records 12jdbc operation transactions
Ruffian Heng embedded bimonthly, issue 49
Wincc7.5 download and installation tutorial (win10 system)
[MySQL] lock
【云原生】手把手教你搭建ferry开源工单系统
C language custom type: struct
随机推荐
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储
Online yaml to CSV tool
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
vulnhub hackme: 1
Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation
LDAP應用篇(4)Jenkins接入
Hcip day 16
Analysis of pointer and array written test questions
Is it safe to open an account in Zheshang futures?
MySQL learning record 10getting started with JDBC
VMware virtualization cluster
China vanadium battery Market Research and future prospects report (2022 Edition)
logback1.3. X configuration details and Practice
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
Configuring OSPF load sharing for Huawei devices
CAD ARX gets the current viewport settings
Chinese Remainder Theorem (Sun Tzu theorem) principle and template code
synchronized 解决共享带来的问题
【MySQL】日志
Colorlog combined with logging to print colored logs