Javascript-based Partial Application Learning

Source: Internet
Author: User

This time I will learn about the Partial Application. Let's take a look at the introduction of functions. On the Wiki, we will briefly introduce them:
In mathematics, a function describes the correspondence between each input value and the unique output value. The symbol is f (x ). For example, expression f (x) = x2 represents a function f, where each input value x is associated with a unique output value x2.

Therefore, if an input value is 3, the corresponding output value is 9. G (x, y) = xy has two parameters x and y, with the product xy as the value. The above describes the function (for the convenience of assuming that x and y are all int), and provides two examples of the function. In another way, f (x) can be expressed: x-> y (x2), that is, after the ing from f to x2, it is written as int-> int.

Accept an int and return an int. If you look at g (x, y) Again, it can be expressed as: x-> y-> z (xy ). That is, x and y are mapped to z through g and written as int-> int. Let's look at the g (x, y) function and use javascript to implement it:
Copy codeThe Code is as follows:
Function g (x, y ){
Return x * y;
}

Perfect. Very close to the definition of mathematics. It accepts two parameters, x and y. And returns the product of the two. However, when x is a constant, for example, x = n (n is a natural number ). Then g (n, y) = ny. This is the product of a constant and a variable. It accepts the ny ing of a parameter y to return ny, that is, y-> z (ny), and writes it as int-> int. Therefore, we can understand the above work. g (x, y) accepts an int parameter and returns a function int-> int. The returned function only accepts one int and returns one int. In javascript:
Copy codeThe Code is as follows:
Var h = g (2 );

H indicates the function h (y) = 2y. In this way, h (5) = 10, h (13) = 26, etc.
Copy codeThe Code is as follows:
H (5 );
H (13 );

This technology is to convert the function form that requires multiple parameters into a function chain that accepts a single parameter. It is usually called Curring, which is the name of Haskell Curry, but he is not the first to propose 1. But unfortunately, javascript does not support this feature. Therefore, it is not complicated to implement such features. It stores the parameters. When the next function on the function chain is called, the forward parameter is taken out and passed to the next function on the chain until the return value is obtained. First, let's look at the following code:
Copy codeThe Code is as follows:
Function atarr (a, index ){
Var index = index | 0, args = new Array (a. length-index );
For (var I in ){
If (I> = index) args [I-index] = a [I];
}
Return args;
}
Function m (scope, fn ){
If (arguments. length <3) return fn. call (scope );
Var p = atarr (arguments, 2 );
Return function (){
Var args = atarr (arguments );
Return fn. apply (scope, p. concat (args ));
}
}

Test code:
Copy codeThe Code is as follows:
Var plus = function (a, B ){
Return a + B;
};
Var plus2 = m (null, plus, 2 );
Console. log (plus2 (10 ));
Console. log (plus2 (0 ));
// Result
12
2

In this way, our goal has been achieved. In the above atarr function, parameters starting at the specified position in the arguments object are retrieved and saved to an array. M function is the main character. It completes the previously defined task, implements the function that saves the parameters on the function chain and accepts the remaining parameters. The plus function in the test code originally accepts the parameters a and B and returns the sum of a and B, that is, int-> int, while plus2 is used to accept the addition of a parameter B and 2, and returns the sum of 2 and B, that is, int-> int.

Through the above work, we have implemented the Partial Application in javascript, and hitch2 implements domain binding and partial in the dojo framework. If you are interested, you can read its source code, which is very simple and clear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.