当前位置:网站首页>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)
边栏推荐
猜你喜欢

ESP系列引脚說明圖匯總

Sort according to a number in a string in a column of CSV file

Circular reference of ES6 module

ROS编译 调用第三方动态库(xxx.so)

JVM performance tuning and practical basic theory - Part 1

C语言 - 位段
![[research materials] 2021 China online high growth white paper - Download attached](/img/51/bea6179e4fac88f8b550b4213a2bca.jpg)
[research materials] 2021 China online high growth white paper - Download attached

What is the use of entering the critical point? How to realize STM32 single chip microcomputer?

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

延迟初始化和密封类
随机推荐
ESP series pin description diagram summary
Hcip day 16
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
MFC sends left click, double click, and right click messages to list controls
JVM performance tuning and practical basic theory - Part 1
指针进阶---指针数组,数组指针
LDAP application (4) Jenkins access
Restore backup data on S3 compatible storage with br
leetcode刷题 (5.28) 哈希表
hcip--mpls
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
matplotlib. Widgets are easy to use
CAD ARX gets the current viewport settings
C language - bit segment
TiDB备份与恢复简介
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
sys. argv
Image fusion -- challenges, opportunities and Countermeasures
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship