Here we talk about another important concept in functional programming, currization

First, let’s make the impure function in the previous code pure by doing the following. You take the mini inside the function.

function checkAge (age) {
    let mini = 18;
    return age >= mini;
}
Copy the code

But there was a problem when we took the mini inside the function, because the value of the variable was equal to a specific number, and there was hard coding, which we all know to avoid when we’re writing programs. It’s easy to hard code, just extract 18 to the parameter position.

function checkAge (min, age) {
    return age >= min;
}

checkAge(18.20);
checkAge(18.21);
checkAge(18.22);
Copy the code

Here we’re done. We always get the same output based on the input, because it’s not dependent on external variables, and there’s no hard coding in it.

As you can see, when we use 18 a lot, this 18 repeats a lot, and we want to avoid this 18 repetition, we can solve this problem by using closures. For example, if we redefine the chekAge function, it receives a reference value min and returns a function.

The function returns an age argument. In the body of the function, we return an age greater than or equal to min. Once defined, we can return a new function checkAge18. This 18 is recorded in the function.

function checkAge (min) {
    return function (age) {
        returnage >= min; }}let checkAge18 = checkAge(18);

checkAge18(20);
checkAge18(21);
checkAge18(22);
Copy the code

You can see here that we will not repeat the reference value in the call because we have already determined it in the first function.

The above method of calling a function is called currization, so let’s briefly explain what currization is.

When we have a function with multiple arguments, we can modify the function. We can call a function, pass only some arguments, and have the function return a new function, which receives the rest of the arguments and returns the corresponding result. This is the currization of the function.

The above code is not generic enough, so let’s take a look at the general Curryization method provided in LoDash. The Curryization method in LoDash is called Curry. This method takes a function as an argument and returns the curryized function. Curry itself is a pure function, and if the argument we passed was a pure function, the returned function would be a pure function as well.

Here we demonstrate the use of the Curry method in Lodash.

Here we define a function that takes the sum of three numbers, which can be converted from a multivariate (number of arguments) function to a unary function. We use Curried to receive the getSum method after currization. This newly obtained curried is called and executed when he judges that the number of arguments passed in is the number he needs. We can pass it all at once, or we can pass it front to back. When a partial argument is passed, it returns a new function.

const _ = require('lodash');

function getSum (a, b, c) {
    return a + b + c;
}

const curried = _curry(getSum);

// curried(1, 2, 3);
// curried(1)(2, 3);
curried(1.2) (3);
Copy the code

So what we found here is that it’s very convenient for us to use a function that we’ve currified, that can pass in one argument, that can pass in multiple arguments.

Lodash below we analogy in the implementation of curry, let’s review before simulation curry method is how to call, when we call this method, we need to give him a pass in a parameter, this parameter is a pure function, when the call is completed he will return a function, that this function is a function of after a curry.

The returned Corrification function can be executed with all or part of the arguments. When all arguments are passed, the function is executed immediately. When partial arguments are passed, a new function is returned and waits to receive the rest of the arguments.

Here we know that the parameters passed are not fixed, so inside the function we need to check whether the number of parameters and the number of parameters passed are the same. We can use ES6’s reset residual parameter (.. Args).

Then we need to compare the number of parameters with the number of arguments to see if they are the same. The argument is args, and the parameter can be obtained by the length of the function name, func.length.

function curry (func) {
    return function curriedFn(. args) {
        if (args.length >= func.length) {
           returnfunc(... args); }else {
            return function () {}}}}Copy the code

When passing a partial argument, we need to merge the current argument with the previous one and compare it with the original argument.

For the newly passed argument, we use… NewArgs gets, previously passed arguments in… The args. Here we can merge args and newArgs, and then call curriedFn manually, and let curriedFn help us determine whether the arguments are equal or not.

function curry (func) {
    return function curriedFn(. args) {
        if (args.length >= func.length) {
           returnfunc(... args); }else {
            return function (. newArgs) {
                returncurriedFn(... args.cancat(newArgs)); }}}}Copy the code

So we’re done here, and finally we’re going to summarize the Currization of functions.

The Currization of a function allows us to pass fewer arguments to a function, resulting in a new function that has memorized some of the fixed arguments. In other words, The currization can realize the step transfer of the parameters of the function. If the parameters passed do not meet the parameter requirements of the function, a new function will be returned, and the subsequent parameters can be passed in the new function. The arguments passed earlier are already recorded in the new function.

Currization internally uses closures to cache the parameters of a function. Currization makes functions more flexible because smaller functions can be generated. And the purpose of this is to use it later when we learn combinations.

Using corrification can transform multivariate functions into unary functions, you can combine these unary functions into more powerful functions.