1. Curry
1. Definition
Currying is the process of converting a function that accepts multiple parameters into a series of functions that accept only a single parameter. The return value of the Curry function is still a function that takes a parameter and returns a new function until all parameters are processed and finally returns the final result.
2. Pros and cons
2.1. Advantages
- Flexibility: Currying can make functions more flexible because it can convert functions with multiple parameters into a series of functions that only accept a single parameter, allowing for more flexibility in combining and using functions.
- Reusability: Currying can make a function more reusable because it can preset some parameters of the Currying function to obtain a new function. This function can be used directly or as a parameter of other functions.
2.2. Disadvantages
- Readability: Currying can make the calling method of functions more complicated, requiring multiple calls to different functions to get the final result, thereby reducing the readability of the code.
3. Applicable scenarios
- Some application functions: When a function needs to pass a part of the parameters, you can use the Curry function to preset the part of the parameters to obtain a new function.
- Simplified parameter passing: When a function requires multiple parameters, you can use the Curry function to convert multiple parameters into a series of functions that accept only a single parameter, thereby simplifying parameter passing.
4. Sample code
4.1. Add up two numbers
Here is a simple curry functionadd
, this function adds two numbers:
function add(a) { return function(b) { return a + b; } } const addFive = add(5); (addFive(2)); // Output 7
In the above code, we define aadd
function, which accepts a numbera
, and return a new function that accepts a numberb
and returna + b
The result. Then we useadd(5)
Get a new functionaddFive
, this function accepts a numberb
and return5 + b
The result. Ultimately, we can useaddFive(2)
Get results7
。
4.2. Currying tool function
Here is a sample code for a simple Curry tool function:
// Currying tool functionfunction curry(fn) { return function curried(...args) { if ( >= ) { return (this, args); } else { return function (...args2) { return (this, (args2)); } } } }
in,fn
It is a function that needs currying. This curry function returns a new function. When the new function receives enough parameters, the original function will be called.fn
, otherwise a new function will be returned and the parameters will continue to be received. In this way, we can use the Curry function to convert multiple parameters into a series of single parameters functions. How to use it is as follows:
function sum(a, b, c) { return a + b + c; } const sum_curried = curry(sum); sum_curried(1, 2, 3); // 6 sum_curried(1, 2)(3); // 6 sum_curried(1)(2, 3); // 6 sum_curried(1)(2)(3); // 6
2. Anti-Curriization
1. Definition
Decurrence is the process of converting a currence function into a function that accepts multiple parameters. The return value of the anti-curry function is a function that takes an object as a parameter, calls the original method of the object and passes the parameter.
2. Pros and cons
2.1. Advantages
- Readability: Decurrence can make the calling method of a function simpler, just call a function and pass an object as a parameter.
- Reusability: Decurrence makes a function more reusable because it converts a function that presets this object into a function that accepts this object, so that the function can be reused on different objects.
2.2. Disadvantages
- Flexibility: Decurization can make the function's this object fixed, thereby reducing the function's flexibility.
3. Applicable scenarios
- Multiplexing function: When multiple objects need to call the same method, the anti-curry function can be used to convert the method into a function that accepts the object as a parameter, so that the function can be multiplexed on different objects.
- Chain Call: When multiple methods require chain calls, the method can be converted into a function that accepts objects as parameters using an anti-curry function, so that chain calls can be conveniently performed.
4. Sample code
4.1 Example 1
Here is a simple anti-curry functionbind
, this function will presetthis
Convert the function of the object to acceptthis
Object's functions:
function bind(fn, obj) { return function(...args) { return (obj, args); } } const obj = { x: 1, y: 2 }; function sum() { return + ; } const boundSum = bind(sum, obj); (boundSum()); // Output 3
In the above code, we define abind
function, this function accepts a functionfn
and an objectobj
and return a new function. This function usesapply
Method calling functionfn
and pass the objectobj
and parametersargs
. Then we usebind(sum, obj)
Get a new functionboundSum
, this function callsum
function and pass objectobj
Asthis
Object. Ultimately, we can useboundSum()
Get results3
。
4.2 Anti-currying tool function
// Method 1 = function() { var self = this; return function() { return (self, arguments); // <==> (arguments) } } // Method 2 = function() { var self = this; return function() { var obj = (arguments); // Intercept the first object return (obj, arguments); } } var push = () var obj = { "length": 1, "0": 1 } push(obj, 2) (obj) // Output { 0: 1, 1: 2, length: 2}
Summarize
This article introducesJavascript
Curriculization and anti-curriculization techniques in. Currying can convert functions that accept multiple parameters into a series of functions that accept only a single parameter, making functions more flexible, reusable and combined. Decurrence can convert currence functions into functions that accept multiple parameters, making the functions more readable and multiplexed. Curriculization and anti-curriculization can be used together to further improve the readability, reusability and composition of the code. In actual development, we can choose to use currying or anti-currying to optimize the code according to specific needs.
The above is a detailed explanation of currying and anti-currying in js. For more information about currying and anti-currying of js, please pay attention to my other related articles!