This is the 26th day of my participation in Gwen Challenge

In computer science, Currying, also translated as carrey or callization, is a technique of converting a function that takes multiple arguments into a function that takes a single argument (the first argument of the original function), and returning a new function that takes the remaining arguments and returns a result.

Let’s talk a little bit about the currization of a function from an interview question

Add (1)(2)(3)(4) returns 10

function add() {
    return Array.from(arguments);
}
Add (1, 2, 3, 4) => [1, 2, 3, 4]
// Convert to the following form
function addCurrying() {
    // do something
}
// call: addCurrying(1)(2)(3)(4) => [1, 2, 3, 4]
Copy the code

Closures take arguments

Closure: A function defined inside a function that statically holds all the inner functions of the parent scope. So each time the value can be saved, at the end of the access to all

So, let’s start with a closure to get all the parameters

var add = function (value) {
    var result = [];
    result.push(value);
    // The _add function is a closure that fetches the value of the parent scope. Because the _add function fetches the parent data, the parent persists
    var _add = function (value2) {
        result.push(value);
        return _add;
    };
    // This method call returns an array
    _add.getResult = function () {
        return result;
    };
    return _add;
};
Copy the code

In this way, the basic Coriolization requirement can be fulfilled by the above function

Perform:

// addCurrying(1)(2)(3)(4).getResult => [1, 2, 3, 4]
// addCurrying(1)(2)(3)(4)
/ / = >
ƒ (value2) {result.push(value); return _add; } * /
Copy the code

Overriding the built-in function returns the result

The above code takes all the parameters, but the result is not automatically returned. Instead, the function is called.

And since the length of the parameter is not known, it is impossible to return the result in time by the length of the parameter array.

So what can be done about it? ?

Of course, in JS functions have prototype chains, so each function inherits some basic methods.

When you define a function, if you print only the name of the function and do not execute it, the internal information of the function is printed.

So we can rewrite this method to execute it after we’re done.

The toString method of the function can print the body of the function, so let’s modify toString to return the result

// First we need an entry function for basic processing
var add = function (value) {
    // Define an array to hold all the parameters passed in
    var result = [];
    // Place the first parameter passed in
    result.push(value);
    // define the function _add to reference the result variable and keep it from being destroyed
    var _add = function (subValue) {
        // push the parameter into result
        result.push(subValue);
        // return function _add for the next call
        return _add;
    };
    // Rewrite the native toString
    _add.toString = function () {
        return result;
    };
    // return _add for the next call
    return _add;
};
Copy the code

The execution result

  • add(1) => [1]
  • Add (1) (2) = > [1, 2]
  • Add (1) (2) (3) = > [1, 2, 3]