Let f(x,y) = x + y, when y = 1, the function can be modified to f(x) = x + 1. The basic idea is to change a binary into a single, and similarly we can reduce a three-variable function to a binary, or even a multi-variable function to a single function.

The Gaussian elimination method in mathematics is somewhat similar to the function coriolization, let’s analyze it in detail.

What is Corrification

The name Currization was coined by Christopher Strachey after the logician Haskell Curry

When a function has multiple arguments, we can modify the function. We can call a function, pass only part of the argument (which will never change), and have the function return a new function. The new function passes the remaining arguments and returns the corresponding result.

The basis of Currization is closures, and not understanding closures can be a barrier to understanding currization. Here’s a look at my previous post about closures.

Let’s look at an example

// Find the sum of a, b and c
function sum(a, b, c) {
  return a + b + c;
}
/ / call
sum(1.2.3); / / 6
Copy the code

I’m going to rewrite it as a Currie function

function sum(a) {
  return function (b) {
    return function(c) {
      returna + b + c; }}}/ / call
let sum1 = sum(1);
let sum2 = sum1(2);
sum2(3); / / 6
Copy the code

If you change the function in this way, it will drive the developer crazy, and the code is not clean and friendly

In actual development, we will probably use more mature function component libraries to carry out the Curryization, such as the next Lodash but curry function

Corrification in Lodash

Call method: _.curry(fn)

  • Function: creates a function that takes one or more parameters from the fn, executes the fn if all the parameters required by the FN are provided, and returns the result of the execution. Otherwise continue to return the function and wait for the remaining arguments to be received.
  • Parameters: functions that require currization
  • Return value: The currified function

We use _. Curry (fn) in Lodash to currize the summation function above

// The parameters are one unary function and two binary functions
// It is possible to transform a function of several variables into a function of one variable
function sum (a, b, c) {
  return a + b + c
}
// Define a Currization function
const curried = _.curry(sum)

// If all parameters are entered, the result is returned immediately
console.log(curried(1.2.3)) / / 6

// If partial arguments are passed, it returns the current function and waits to receive the rest of the arguments in sum
console.log(curried(1) (2.3)) / / 6
console.log(curried(1.2) (3)) / / 6
Copy the code

The Currization function in handwritten Lodash

Let’s first analyze the characteristics of _. Curry (sum) above

  • Taking a pure function parameter sum, returns the curried function

  • If curried is passed the same number of parameters as sum, and all of them are passed at once, curried executes immediately

  • If the number of curried arguments passed in is less than the sum required, a new function is returned and will not be executed until the rest of the required arguments are passed in. If the required number is still not passed, the process continues, returning a new function until all arguments are received

function curry(fn) {
  // curriedFn is a new function for currization
  // Why not use anonymous functions? If args. Length is less than the number of parameters fn.length, we need to recurse again
  return function curriedFn(. args) {
    if (args.length < fn.length){
      return function() {
        // The previously passed parameters are stored in args
        // The new function takes arguments in arguments, since arguments are not really arrays requires array.from () to be converted to an Array
        // Execute recursively, repeating the previous process
        returncurriedFn(... args.concat(Array.from(arguments)))}}returnfn(... args) } }Copy the code

A case study of corrification

In our work, we often meet various requirements for regular verification, such as mobile phone number and email verification when submitting user information.

Let’s wrap a checksum function

// Non-Currified version
function checkByRegExp(regExp,string) {
    return regExp.test(string);  
}

checkByRegExp(/^1\d{10}$/.'15010001000'); // Verify the phone number
checkByRegExp(/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/.'[email protected]'); // Verify the mailbox

Copy the code

In practice, we do not need to pay attention to how the re is matched. We only need to use more specific functions to verify the mobile phone number or email address respectively. Here we need to use the Kerrization function to deal with it.

// Perform currization
let _check = curry(checkByRegExp);
// Generate utility functions to verify phone numbers
let checkCellPhone = _check(/^1\d{10}$/);
// Generate utility functions to validate the mailbox
let checkEmail = _check(/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/);

checkCellPhone('18642838455'); // Verify the phone number
checkCellPhone('13109840560'); // Verify the phone number
checkCellPhone('13204061212'); // Verify the phone number

checkEmail('[email protected]'); // Verify the mailbox
checkEmail('[email protected]'); // Verify the mailbox
checkEmail('[email protected]'); // Verify the mailbox

Copy the code

As a small summary of the above ideas, the advantage of currization is that we can reuse our functions to the maximum extent possible.

Resources: llh911001. Gitbooks. IO/mostly – at… Juejin. Cn/post / 684490… Juejin. Cn/post / 684490… Juejin. Cn/post / 689182…