js-function currying

Source: Internet
Author: User

From the P49, "JavaScript design patterns and development practices",

Function currying is also called partial evaluation. A currying function will first accept some parameters, after accepting these parameters, the function will not be evaluated immediately, but continue to return another function, just passed the parameter in the function formed in the closure is saved. Until the function really needs to be evaluated, the previously passed parameters are evaluated once.

Suppose we're going to write a function that calculates the monthly cost. Before the end of each day, we have to record how much money is spent today, the code is as follows:

var monthlycost = 0; var function (Money) {    + = Money ;}; Cost (100); // first day overhead cost (200); // The next day costs cost (300); // Third day overhead // cost (700);//30th Day expenses alert (monthlycost); // Output:

This code records and calculates how much money is spent on the end of the day, but we don't care much about what we spend each day, and we just want to know how much is spent at the end of the month, that is, only one calculation at the end of the month.

If in the first 29 days of each month we would just save the day's expenses until the 30th genius was evaluated, and that would be our goal. The following code is not a complete implementation of a currying function, but it helps us understand its thinking:

varCost = (function(){    varargs = []; return function(){        if(Arguments.length = = 0){            varMoney = 0;  for(vari = 0, L = args.length; I < L; i++) { money+=Args[i]; }            returnMoney ; }Else{[].push.apply (args, arguments); }}} (); Cost (100);//not really evaluatedCost (200);//not really evaluatedCost (300);//not really evaluatedConsole.log (Cost ());//Evaluation and output:

Next, write a generic function currying () {}, which takes a parameter, which is the function that will be currying. In this example, the function is to traverse the cost of the day of the month and find out the sum of them. The code is as follows:

var function (FN) {    var args = [];         return function () {        if(arguments.length = = = 0) {            return fn.apply (this  , args);        } Else {            [].push.apply (args, arguments);             return Arguments.callee;}}    ;
varCost = (function(){    varMoney = 0; return function(){         for(vari = 0, L = arguments.length; I < L; i++) { money+=Arguments[i]; }        returnMoney ; }}());varCost = currying (cost);//Convert to currying functionCost (100);//not really evaluatedCost (200);//not really evaluatedCost (300);//not really evaluatedConsole.log (Cost ());//Evaluation and output:

At this point, we have completed the writing of a currying function, and when the cost () is called, if the parameters are explicitly taken, it means that the actual evaluation is not done at this time, but rather the parameters are saved, and the cost () function returns another function. It is only when we execute cost () in the form of no parameters that we really start evaluating calculations by taking advantage of all the previously saved parameters.

From the P49, "JavaScript design patterns and development practices",

js-function currying

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.