Reprint Please specify source: http://www.cnblogs.com/shamoyuu/p/currying.html
What is the currying/curry of JS function?
When it comes to the curry of JS, I believe many friends will head big. or not very clear. Let me give you a brief introduction today.
I use a sentence to summarize the functionof curry, JS Curry is to gradually pass the parameters, gradually reduce the scope of the function, and gradually solve the process .
Perhaps you are not very clear about this sentence, then, let's look at a case, briefly explain:
Requirements: We write a function that adds several parameters to the function and returns the result! So the functions we wrote are as follows
var concat3words = function (A, B, c) { return a+b+c;};
What about the function of curry? is a partial solution, first pass a parameter, then pass a b parameter, then pass a C parameter, and finally add the three parameters!
var concat3wordscurrying = function (a) { return function (b) { return Function (c) { return a+b+C; }; };};
We look at the output:
Console.log (Concat3words ("Foo","Bar","Baza"));//Foo Bar BazaConsole.log (Concat3wordscurrying ("Foo"));//[Function]Console.log (Concat3wordscurrying ("Foo")("Bar")("Baza"));//Foo bar baza function chain Call
The case of curry
The above requirements we change a little bit. Now we further, if the request can be passed more than 3 parameters, you can pass any number of parameters, when the parameter is not passed the output results? Now let's use the curry to make it simple:
varAdder =function () {var_args = []; returnfunction () {if(Arguments.length = = =0) { return_args.reduce (function (A, b) {returnA +b; }); } [].push.apply (_args, [].slice.call (arguments)); returnArguments.callee; }}; varsum =adder (); Console.log (sum); //Functionsum ( -, $)( -);//flexible invocation, one or more arguments can be entered in a single call, and chained calls are supportedSum -); Console.log (sum ()); //1000 (plus total calculation)
The above adder is a function of the Gerty, it returns a new function, the new function receives a sub-batch to accept the new parameters, deferred to the last calculation. We can arbitrarily pass in the parameters, when the parameters are not passed, the output calculation results!
The arguments of the popularization of basic knowledge
See above the curry realization, maybe some friends will be a bit dizzy, in fact, there is no new knowledge, if you read the article I wrote before, I believe I can understand! Apply,call was introduced before. The fourth article in the front-end dry goods, also has the mention! Only arguments in front of the article did not introduce.
The arguments object is a more specific object and is actually a built-in property of the current function. Arguments is very similar to an array, but is not actually an array instance.
Let's take a look at the following example:
function f (A, B, c) {alert (arguments.length); //Result: "2"A = -; Alert (arguments[0]);//Result: "arguments[0] ="haorooms"; alert (a); //Result: "Haorooms"alert (c);//result: "Undefined"c = .; Alert (arguments[2]);//result: "Undefined"}f (1,2);
We usually use
1)
Converts an incoming parameter to an array. Slice is a function of JS, about its usage, not to check! The above notation is equivalent to Array.prototype.slice.call (arguments, 1);
In addition, there is a very useful property in the arguments object: callee. Arguments.callee returns the current function reference where this arguments object resides. It is recommended to use Arguments.callee instead of the function name itself when using recursive function calls. Arguments is introduced here!
The common curry function
The following is a common curry function.
var currying = function (fn) { var _args = []; return function () { if0) { return fn.apply ( this , _args); } Array.prototype.push.apply (_args, [].slice.call (arguments)); return Arguments.callee; }};
We can simply apply the currying by using the following function:
varMulti=function () {varTotal =0; for(vari =0C c = arguments[i++];) { Total+=C; } returnTotal ;};varsum =currying (multi); SUM ( -, $)( -); sum ( -); Console.log (sum ()); //1000 (true calculation when blank call)
Currying for increased applicability
Above my introduction is to curry delay execution, narrow the scope! There is also an increase in the applicability of the function, but it also reduces the scope of application of the function. The general wording is as follows:
function currying (FN) { var slice = Array.prototype.slice, 1); return function () { var __inargs = slice.call (arguments); return fn.apply (null, __args.concat (__inargs));} ;}
For example:
function Square (i) {returnIi;} function map (Handeler, list) {returnList.map (Handeler);}//the square of each item of the arrayMap (Square, [1,2,3,4,5]); map (square, [6,7,8,9,Ten]); map (square, [Ten, -, -, +, -]);
example, a map general function was created to adapt to different scenarios. Obviously, versatility doesn't have to be doubted. At the same time, the same processing function is repeated in the example: square.
Curry Transformation:
function Square (i) {returnIi;} function map (Handeler, list) {returnList.map (Handeler);}varMAPSQ =currying (map, square); MAPSQ ([1,2,3,4,5]); MAPSQ ([6,7,8,9,Ten]); MAPSQ ([Ten, -, -, +, -]);
Function.prototype.bind method is also the application of curry
Unlike the Call/apply method, the Bind method sets the first parameter to the context of the function execution, and the other parameters are passed to the calling method in turn (the body of the function does not execute itself, can be seen as deferred execution), and the dynamic creation returns a new function, which is in line with the curry feature.
var 888 }; var bar = function () { console.log (this// bind Bar ();
The end, the scattered flowers
The curry of JavaScript functions (currying)