What is the Currization of a function

Function corrification is the splitting of multiple parameters of a function into a function that receives less than the original number of parameters (usually one). The function then returns as a function and can continue to take arguments until no arguments are passed in or the arguments passed in are equal to the defined arguments.

Consider some examples:

The normal add function:

function add (a, b) {
    return a + b
}

add(1.1) / / 2
Copy the code

Change the function to Currization:

function add (a) {
    return function (b) {
        return a + b
    }
}

add(1) (1) / / 2
Copy the code

This is just a very simple version, just to give you a sense of what curryization is.

Now that we know about Cremation, let’s see how we can improve it.

An improved currization of the function

As you can see, the add function can only pass two parameters. If you pass more parameters, it will not work. If you pass fewer parameters, it will not work

The add function I’m looking forward to doesn’t need fixed arguments and can change dynamically

The first way to write it

function curry(fn) {
    const len = fn.length
    return function anonymous(. args) {
        if(args.length >= len) {
           return fn.apply(null, args)
        } else {
           return anonymous.bind(this. args) } } }function add (a,b,c) {
    return a + b + c
}
add = curry(add)
add(1) (1) (1) / / 3
add(1) (1.1) / / 3
add()(1.1.1) / / 3
Copy the code

Resolution:

  • Here you pass the add function as an argument to Curry, record the length of the add function (which takes several arguments), and return a new function
  • In the new function, if the parameter passed to the function is greater than or equal to the length of the add function, execute the function directly
  • If less than, the function continues to be returned. Until the function passes in arguments greater than or equal to the length of the add function, at which point the function is executed, and then returns.

This function is based on the parameters defined by add. If you define two parameters, it will be executed. If you define three parameters, it will be executed.

This is the number of parameters you need to define when you need to define the add function.

What if I don’t want to specify the number of arguments when I define add?

The answer is yes.

The second way to write it

function curry(fn) {
    let args = []
    return function anonymous() {
        if(arguments.length === 0) {
           const params = [...args]
           args = []
           return fn.apply(null, params)
        } else{ args.push(... arguments)return anonymous
        }
     }
}
function add (. args) {
   return args.reduce((acc, prev) = > acc + prev)
}
add = curry(add)
add(1) (1) (1) ()/ / 3
add(1) (1.1) ()/ / 3
Copy the code

Resolution:

This notation allows the add function to take an indefinite argument, based on the fact that the function is executed with an empty argument. So when you pass an argument and then add an empty argument to the execution, you’re done.

Let’s look at another way of writing it:

The third way to write it

function add(. args) {
  let fn = function (. args1) { args.push(... args1)return fn 
  }
  fn.toString = function () {
    return args.reduce((acc, prev) = > acc + prev)
  }
  return fn
}
add(1) (1) = =2 // true
add(1.1) = =2 // true
add(1) (1) (1) = =3 // true
add(1) (1.1) = =3 // true
add(1) (1.1).toString() / / 3
+add(1) (1.1) / / 3
add(1) (1.1) + ' ' / / 3
Copy the code

Add generates a new function, overwrites the toString method of the new function and returns the new function.

So calling add is always a return function, and if you console it out as a function, because you overwrote the toString method, you can either use the privacy conversion (check out my previous article if you don’t know about the privacy conversion) or call the toString method to get the desired value.

conclusion

So that’s a couple of ways to write the Currization of a function, and it’s not limited to just one way to write it, it can be flexible. We can all look at learning, find suitable for their own. I hope it was helpful.