JavaScript function currying

Source: Internet
Author: User

function type

JavaScript is a functional language that takes a function as a class citizen. Functions are also objects in JavaScript (inheritance function), and functions can be passed as arguments to function variables. In recent years, the function has been in high concurrency because of its non-side effects, transparency, lazy computation, etc., and the Big data field has been fired up.

JavaScript is also like underscore, lodash and other functional libraries, such as the use of Lodash:

var names = _.chain (users)  . Map (function (user) {    return user.user;  })  . Join (",")  . Value (); Console.log (names);

For more information on Lodash, refer to the Lodash of the JavaScript tool library.

Currying

Today's article will look at the functional capabilities of JavaScript in the form of curry in higher-order functions.

In computer science, currying is a technique that transforms a function that accepts multiple parameters into a function that takes a single parameter (the first parameter of the original function) and returns a new function that takes the remaining parameters and returns the result. This technique was named by Christopher Strachey, a logic home Curry, although it was invented by Moses Schnfinkel and Gottlob Frege.

In theoretical computer science, Curry provides a way to study functions with multiple parameters in a simple theoretical model, such as a lambda calculus that accepts only a single parameter.

The realization of the curry of JavaScript

In the following example, we will generalize the method of the curry to accept any parameter until the number of declared method parameters is saturated before executing, so there can be a number of curry functions depending on the number of arguments.

The code is as follows:

(function (global) {var Fn_args =/^function\s*[^\ (]*\ (\s* ([^\)]*)/m, fn_arg_split =/,/, Fn_arg =/^\s *(_?) (\s+?) \1\s*$/, strip_comments =/((\/\/.*$) | (    \/\*[\s\s]*?\*\/))/mg;        var getarglength = function (func) {var fntext = func.tostring (). Replace (strip_comments, ");        var argdecl = Fntext.match (Fn_args);        var params = [];                Argdecl[1].split (Fn_arg_split). ForEach (function (ARG) {arg.replace (Fn_arg, function (all, underscore, name) {            Params.push (name);        });        });    return params.length;    };        var curryfunc = function (func, len) {len = Len | | getarglength (func);        var args = [];        if (len = = = 0) {return func.apply (null);            } return function () {[].push.apply (args, [].slice.apply (arguments)];            if (args.length >= len) {return func.apply (null, args);        } return Arguments.callee;  };  }; Global.curryfunc = Curryfunc;}) (this); function add (x, Y, z) {return x + y + z;} Console.log ("Result 1:", Curryfunc (add) (1, 2) (3)), Console.log ("Result 2:", Curryfunc (add) (1) (2, 3)); Console.log (" Result 3: ", Curryfunc (add) (1) (3) (2)); function add (x, Y, z) {return x * y * z;} Console.log ("Result 1:", Curryfunc (Add) (2, 4) (6)), Console.log ("Result 2:", Curryfunc (Add) (2) (4, 6)); Console.log (" Result 3: ", Curryfunc (Add) (2) (6) (4)); function SayHello () {return" Hello ";} Console.log (Curryfunc (SayHello));

First, the above will use the regular to get the number of arguments passed in the function. Returns the proxy for a function, each time a call is cached in the args temporary variable, until the number of parameters is saturated to execute immediately. Code is lengthy, slowly taste, of course, there is insufficient support, but also hope that we point out.

JavaScript function currying

Related Article

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.