What is a partial function
In computer science, partial function applications refer to the process of fixing some parameters to one function to produce another smaller function.
Here’s an example:
// We created a function to multiply
function mult(a, b) {
return a * b;
};Copy the code
Based on this function, we use bind to create a new function double:
let double = mult.bind(null.2);
console.log( double(3));// mult(2, 3) = 6;
console.log( double(4));// mult(2, 4) = 8;
console.log( double(5));// mult(2, 5) = 10; Copy the code
Mult.bind (null, 2) creates a new function double that is called to mult using null as context and 2 as the first argument. Other parameters wait to be passed in.
This is the partial function application — we create a new function and replace some of the parameters with specific values.
When to use partial functions
We just created a new function double based on mult. Here we create a new function triple:
let triple = mult.bind(null.3);
console.log( triple(3));// mult(3, 3) = 9;
console.log( triple(4));// mult(3, 4) = 12;
console.log( triple(5));// mult(3, 5) = 15;Copy the code
One thing we can benefit from using partial functions is that we create a separate non-anonymous function (double, triple). We can use it without passing in the first argument every time, because bind takes care of it for us.
Partial functions are also useful in other scenarios where we have a very general function and want to easily retrieve a particular variant of it.
For example, we have the function POST (Signature, API, data). The request method is called with the same user signature, and here we want to use a partial variant: POST (API, data), indicating that the request was sent from the same user signature.
What is Corrification
Sometimes people confuse the use of partial functions with another thing called Currization, but that’s another interesting technique that has to do with functions.
In functional programming languages and many other languages, currying provides a way to automatically manage how arguments are passed to functions and exceptions.
Simply put, currying is a technique that converts a function of the form F (a, b, c) into a call of the form F (a)(b)(c).
Here’s an example:
function currying(fn) {
return function(a) {
return function(b) {
return fn(a, b);
};
};
}
/ / usage
function sum(a + b) {
return a + b;
}
let carriedSum = currying(sum);
console.log( carriedSum(1) (2));/ / 3Copy the code
As you can see from the above example, the implementation of carrying is a series of encapsulation.
-
currying(fn)
The result is a layer of encapsulationfunction(a)
. - When it’s called, it’s like
carriedSum(1)
In this way, its arguments are saved toLexical environment, and returns a new layer of encapsulationfunction(b)
. - then
carriedSum(1)(2)
callfunction(b)
, passing in parameters2
, which calls the multi-parameter function passed to the initialsum
.
Sum is a variant of the partial function that is returned by passing arguments one by one after curryization.
This is probably one of the reasons why it’s easy to confuse the two concepts.
Higher Corrification
Advanced currization allows functions to be called and partial functions to be retrieved.
We can implement an advanced Currization:
function currying(fn) {
return function curried(. args) {
if(args.length>=fn.length) {
If the length of the argument passed is greater than the length of the initial function parameter, the initial function is executed directly
return fn.apply(this, args);
} else {
// Otherwise get a partial function, recurse the carried method until all arguments are obtained, and execute directly
return function(. args2) {
return curried.apply(this, args.concat(args2)); }}}}function sum(a, b, c) {
return a + b + c;
}
let curriedSum = currying(sum);
// General call
console.log( curriedSum(1.2.3));/ / 6
// Get the partial function of curriedSum(1) and call it with the other two arguments
console.log( curriedSum(1) (2.3));/ / 6
// Complete currie call
console.log( curriedSum(1) (2) (3));/ / 6Copy the code
What is the purpose of Currification?
As can be seen from the example of the high-level Corrification implementation above:
-
sum
Function in theCurrie,It has no effect on usage afterwards and can still be called normally. - In many cases, we can more easily generate partial function variants, which can be used more flexibly.
In general, the purpose of Curryization is to make it easier for us to obtain partial function variants of the initial function without affecting the call form of the initial function.
Finally, you are welcome to leave a message and discuss with each other.