What is the cosmology of a function

Currying, also called partial evaluation of a function, is a technique that converts a function that takes multiple arguments into a function that takes a single argument (the first argument of the original function), and returns a new function that takes the remaining arguments and returns a result.

In layman’s terms:

  • Currization is a function that takes multiple arguments, passes in only one argument, generates a new function, and the new function takes the remaining arguments to run the result.
  • A similar concept to currization: higher-order functions (see my previous article on higher-order functions)

JavaScript advanced programming says:

  • A Currified function is typically created dynamically by calling another function and passing it the function to be currified and the necessary arguments.

  • General examples of high-level programming:

function curry(fn) {
    // Get the remaining parameters to convert the class array to an array
    var args = Array.prototype.slice.call(arguments.1);
    return function() {
        var innerArgs = Array.prototype.slice.call(arguments);
        var finalArgs = args.contact(innerArgs);
        return fn.apply(null, finalArgs); }}function add (num1, num2) {
    return num1 + num2;
}
var curriedAdd = curry(add, 5);
alert(curriedAdd(3); / / 8
Copy the code

This may not be easy to understand at the beginning, but let’s go step by step to explain the steps of the realization of chemical chemistry and what problems it solves

So now we have A function A

function A(a, b, c) {
   return a + b + c;
}
Copy the code

If we have A wrapped function curry, it takes A as an argument and can convert A to A function. The result returned is the converted function

var _A = currying(A);
Copy the code

As A function, _A can process the remaining parameters of A. Therefore, the following results are required to be equivalent:

_A(1.2.3);
_A(1.2) (3);
_A(1) (2.3);
_A(1) (2) (3);
A(1.2.3);
Copy the code

In a simple scenario, we can visually encapsulate the kerochemical function as follows

function myA(a) {
    return function(b) {
      return function(c) {
        returna + b + c; }}}Copy the code

So the following operation is really equivalent

A(1.2.3);
myA(1) (2) (3);
Copy the code

However, according to the requirements, myA(1, 2)(3) is also required to be equivalent to other values, but it is not. Therefore, the degree of freedom of the coriolical function encapsulated in this way is low, and some requirements cannot be met

So we need to know how to encapsulate a cosmological general formula, right

Encapsulation is as follows:

function curry(fn, args) {
    args = args || []
    var arity = fn.length
    
    return function() {
        var _args = Array.prototype.slice.call(arguments)
        Array.prototype.unshift.call(_args, ... args) _args = _args.concat(args)if (_args.length < arity) {
            return currying.call(null, fn, _args)
        }
        
        return fn.apply(null, _args)
    }
}
Copy the code

Pits encountered (critical)

I encountered some pits when I wrote the function kerochemistry, although not big, but if I did not pay attention to it, I would still fall down, since I fell down, so I will record to prevent everyone from falling down

Function length: function.length

I’m asking you, do you know the length of the function?

What? And the function has length, right? It’s just strings. Does an array have length?

Yes, a function has length, and the length of the function is the number of parameters it must pass in

The difference between a parameter, which corresponds to a variable defined in a function, and an argument, which is passed in during a function call at runtime, is defined in Javascript’s Definitive Guide. To be clear, parameters are the variables that a function declares, and arguments are the specific arguments that we pass in when we call the function.

So let’s look at an example

var fn = function(a, b) {}
console.log(fn.length);
Copy the code

Those of you who don’t know the length of a function probably don’t know the answer to this question, but according to the above, there are two parameters. Well, the answer is 2. Ok, let’s go to the next problem

var fn = function(a, b = []) {}
console.log(fn.length);
Copy the code

How much do I print for this? Some people are a little confused, I think it’s still 2, I thought it was, but the correct answer is 1, so why is that

The length of a function is the number of arguments that the function must pass, but the number of parameters does not include the number of remaining arguments, only the number of arguments before the first one has a default value. Arguments. length is the number of actual arguments that the function is called

Ok, so once you know that you can copy the following code into your browser console and paste it to see the answer, just to get a better impression

function fn1(a = () => {}, b = [], c) {}
function fn2(a, b = [], c) {}
function fn3(. args) {}
function fn4() { alert(arguments.length) }

console.log(fn1.length); / / 0
console.log(fn2.length); / / 1
console.log(fn3.length); / / 0
console.log(fn4.length); / / 0
Copy the code