JavaScript functional programming - currying

  • Currying principle
  • How currying
  • Currying applications

 First, the principle of currying

Currying: mathematics and computer science, currying conversion using a plurality of parameters is a function of a parameter into a series of techniques used functions.

Curried front-end main purpose should be to simplify the structure of the code, to improve the maintainability of the system, a method, only one parameter to force the unity function, it is natural to do cohesion function, reduce coupling.

Currying advantages: reducing duplicated code, the code to improve the adaptability.

Curried basic principle, the function performed first split into two passages plurality of reference parameters to execute:

1  // There are four parameters as a function of 
2  function the Add (A, B, C, D) {
 . 3      return A + B + C + D;    
 . 4  }
 . 5  // realize a method of fixing two currying parameter passing 
. 6  function FixedParmasCurry (FU) {
 . 7      var _arg = Array.prototype.slice.call (arguments,. 1); // Get currying incoming initial parameters 
. 8      return  function () {
 . 9          var newArg _arg.concat = ( Array.prototype.call (arguments, 0)); // splicing parameters currently executed 
10          return fn.apply ( the this , newArg);
 . 11      }
 12 is }
 13  // test a 
14  var foo = FixedParmasCurry (the Add, 1,2 );
 15 the console.log (foo (3,4-)); // 10 
16  // Test Two 
. 17  var Fun = FixedParmasCurry (the Add,. 1 ) ;
 18 is the console.log (Fun (2,3,4)); // 10

But that did not achieve true curried, real curried should pass any parameters each time, through any number of times to perform the function, the function returns the result of execution until the argument to the function parameters to achieve the required time; it can be said each time the function is passed the appropriate parameters, return results of each execution stage. Why do you say? Currie analytic applications in detail.

 Second, how currying

1  // achieve currying 
2  function add (A, B, C, D) {
 . 3      return A + B + C + D;    
 . 4  }
 . 5  // real needs curried add function this should be the following: 
. 6  var = newAdd Curry (the Add);
 . 7 newAdd (1,2,3,4 );
 . 8 newAdd (. 1) (2) (. 3) (. 4 );
 . 9 newAdd (1,2) (3,4- );
 10 newAdd ( 1,2) (. 3) (. 4 );
 . 11 newAdd (l, 2,3) (. 4 );
 12 is newAdd (. 1) (2) (3,4- );
 13 is newAdd (. 1) (2,3,4 ) ;
 14  //That is true Curry performed above can be implemented in any combination of 
15  
16  function Curry (Fn, length) {
 . 17      var length = length || fn.length;
 18 is      return  function () {
 . 19          IF (The arguments.length < length) {
 20 is              var Combined = [Fn] .concat (Array.prototype.slice.call (arguments, 0 ));
 21 is              return Curry (FixedParmasCurry.apply ( the this , Combined), length - The arguments.length);
 22 is          } the else {
 23 is              return fn.apply ( the this , arguments);
 24          }
25      }
 26 is  }
 27  
28  function FixedParmasCurry (Fn) {
 29      var _arg = Array.prototype.slice.call (arguments,. 1); // Get incoming initial parameter currying 
30      return  function () {
 31 is          var newArg _arg.concat = (Array.prototype.slice.call (arguments, 0)); // splicing parameters currently executed 
32          return fn.apply ( the this , newArg);
 33 is      }
 34 is }

Achieve Analysis: FixedParmasCurry (fn) has essentially the incoming parameters as a function of the parameters necessary to perform direct execution, lack of judgment on the parameter length. Initialization function to achieve the overall length of currying necessary length parameter; each calculation is then passed into the function required length, until the length of passed parameters arguments.length parameter is greater than or equal to the desired length, you can perform true the function (line 23).

 Third, the application Curry

 The following simulation example ajax request requirements:

. 1  function Ajax (type, URL, Data) {
 2      var XHR = new new the XMLHTTPRequest ();
 . 3      xhr.open (type, URL, to true );
 . 4      xhr.send (Data);
 . 5  }
 . 6  // if there is a demand for simultaneously transmitting a parameter to the same three connections, following this approach appears obvious code redundancy 
. 7 Ajax ( 'the POST', 'www.test.com', 'KEYIN name =' );
 . 8 Ajax ( 'the POST', 'www.test2.com', 'KEYIN name =' );
 . 9 Ajax ( 'the POST', 'www.test3.com', 'KEYIN name =' );
 10  // Curry 
. 11  var ajaxCurry = Curry (Ajax);
 12  var post = ajaxCurry("POST");
13 post('www.test.com','name=keyin');
14 post('www.test.com2','name=keyin');
15 post('www.test.com3','name=keyin');

May not be so obvious in the example, it is conceivable at least one thing, when a portion of the plurality of service functions is consistent with the underlying implementation, if by introducing parameters such as a common example, in addition to reducing the coupling of the code, but also the flexibility dismantling merge functional requirements.

 

Guess you like

Origin www.cnblogs.com/ZheOneAndOnly/p/11367224.html