The partial function in JavaScript--essential functional Programming + closure, return function

Source: Internet
Author: User

Unless you have used other functional programming, you may be unfamiliar with the following two concepts: "Partial function Application" and "function curry".

偏函数应用,英文是partial application,也可以译作“局部应用”、“部分应用”、“偏应用”函数柯里化,英文是currying,也可以译作“局部套用”、“加里化”、“卡瑞化”

That being said, if you understand these two concepts, you can use them in your own code.

Functions

Even if you are already familiar with JavaScript's function, you know that function can be used as a return value and can be used as a parameter.
But I recommend you read this first part. If you don't need it, you can skip it and look directly at the section of the local app.

Let's start by looking at a very basic example:

function add(a, b) { return a + b;}add(1, 2); // 3add(1, 3); // 4add(1, 10); // 11add(1, 9000); // 9001

Although the above example is very simple, but also demonstrates that we repeatedly invoke a function of the scene, and each pass in the first parameter is the same, in the above code is the number 1

Functions return functions

We can also create a function called Makeadder, which returns another function that can pass in the parameter. (like this function, which can be used to create other function or object, usually called a factory-factories)

The returned function, if passed inside the parameter call, can return the value of the passed parameter and the value of the original parameter, returning the result of the addition.

More general function.functionAdd(A, B) {return a + b;} Add1,2);3add (10, 3); //13//more specific function Generator. function makeadder (a) {function (b) {//more specific functions. var addone = Makeadder (1) AddOne (2); //3addOne (3); //4var addten = Makeadder (10); Addten (2); //12addTen (3); //             

The code above works because JavaScript supports the functionality of closures, and because of the existence of closures, the function can access variables outside of the function, even the outer layers of the scope when the function is called.

In addition, in JavaScript, function is a class citizen. Because of this, function can accept function as a parameter, or it can return function.

Closures and functions This male name often works together: The returned function can continue to use the passed arguments.

The above code gives us some convenience, we can no longer use Add (1, 2), and directly use AddOne (2), but this does not have to pay a price to achieve this.

First, the logic of the actual addition is repeated in the code of the above two examples, and there are still some problems.

Secondly, for each addition scenario similar to the above, in this way to separate the different logic, we will need to manually create the Makesomething factory function.

Functions accepts functions

The following logical step is to create a more general factory function that accepts not only a parameter to bind, but also a function, which contains all the core logic. (function that is passed to other functions is generally called a callback function-callbacks)

In this way, a separate factory function can be used to create a function binding.

Note that the original function has not been updated and their behavior will not change. They are easily called.

Relatively flexible, more specific function generator.functionBindfirstarg(FN, a) {Returnfunction(b) {RETURN FN (A, b); };}More general functions.functionAdd(A, B) {return a + b;} Add1,2);3functionMultiply(A, B) {return a * b;} Multiply10, 2); //20//more specific functions. var AddOne = Bindfirstarg (add, 1); AddOne (2); //3addOne (3); //4addOne (10); //11addOne (9000); //9001var multiplybyten = Bindfirstarg (multiply,  Multiplybyten (2); //20multiplyByTen (3); //30multiplyByTen (10); //100multiplyByTen (9000); //90000              

The highlight of the above code is not that it can bind a parameter to any function, and that it can bind the method to its own body as the first parameter of the binding function, so that a bindable function is created.

Think of this situation: if Bindfirstarg can bind the first parameter to a function, but this function can accept two parameters, such as passing in 1 and 10,1 to add, 10 to multiply.
Bindfirstarg can accept two 2 parameters, if the first parameter is itself, that is, bindfirstarg can be used to bind itself.

Look at the following example:

// More specific function generator.var makeAdder = bindFirstArg(bindFirstArg, add);// More specific functions.var addOne = makeAdder(1);addOne(2); // 3addOne(3); // 4var addTen = makeAdder(10);addTen(2); // 12addTen(3); // 13

The partial function in JavaScript--essential functional Programming + closure, return function

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.