This is the 13th day of my participation in the August More Text Challenge

The first time I came into contact with the concept of Currization function was in an interview many years ago. At that time, I did not know the concept of Currization, and I used closure to solve the problem by violence 😄. After solving the problem by violence, I always felt that the solution was not right, so I searched online

Currie,

What is Corrification?

In computer science, Currying 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. The technique was named after logician Haskell Curry by Christopher Strachey, although it was invented by Moses Schnfinkel and Gottlob Frege.

Intuitively, Corrification states that “if you fix some parameters, you’ll get a function that takes the rest.” So the function yx with two variables, if you fix y equals 2, you get the function 2x with one variable.

In the absence of the use of coriolization, to achieve a trisomal summation function

const add = (a, b, c) = > {
  return a + b + c;
};
Copy the code

The call method is relatively simple, and all parameters need to be passed at once to get the desired result

add(1.2.3)
Copy the code

One of the advantages of Currization is to wrap a function as a function that can be called several times, and not return the result until the desired parameters are reached

Start by implementing a simple version of the Currization function

const currying = (fn, ... outParams) = > {
  // Get the number of arguments required by the fn function
  const paramsLen = fn.length;

  return (. args) = > {
    // Collect all parameters
    let params = [...outParams, ...args];
    // If the parameter does not reach the parameter required by fn, continue to collect parameters
    if (params.length < paramsLen) {
      returncurrying(fn, ... params); }returnfn(... params); }; };Copy the code

The way to use it is very simple

let newAdd = currying(add);
newAdd(1.2) (3); / / 6
newAdd(2.4.6); / / 12
Copy the code

advantage

Currization has some advantages over ordinary functions: delayed operation and parameter reuse

Delay to run

The above summation of three numbers can well reflect the optimization of the coriolization function’s delayed operation, which can wait until the required parameters are collected before executing the function to obtain the result

Parameters of reuse

Now you need to write a function that determines the data type

/** * type: type - [object Array], [object Number] * source: data source */
const judgeType = (type, source) = > {
  return Object.prototype.toString.call(source) === type
}
Copy the code

When used: Check whether the Array type is used

judgeType('[object Array]'[]),// true
judgeType('[object Array]'.123) // false
Copy the code

At first glance, everything looks fine, but when using the judgeType function, there are some problems

Inconvenience: Every time the judgeType function is called, not only the data source needs to be passed, but also the data type needs to be passed, which greatly increases the inconvenience of use

Unstable: When passing type, it may be found that [Object Array], [Object Number] and other parameters are long and tedious, and it is easy to write wrong parameters accidentally, resulting in unstable results

The judgeType is currified to determine whether it is an Array type

const isArray = currying(judgeType, '[object Array]')
Copy the code

**'[object Array]’** A parameter that is being reused only needs to be passed once. Subsequent calls will use this parameter

Once you get the isArray function, you just need to pass the data source each time you call it

isArray([]) // true
isArray(' ') // false
Copy the code

You can see that function calls are more convenient, semantic, and stable after processing