Function curry (Haskell Curry)

Function curry (Haskell Curry)

What is function curry?

The official explanation Currying, also known as Partial Evaluation, is to transform a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns to accept the rest The technique of a new function that parameters and returns the result.

Personal Vernacular Comprehension

When a function fn has multiple parameters, a part of the parameters can be passed in first to generate a relay function nextFn, and then the remaining parameters can be passed in nextFn. (One-step curry)

function currying(x, y) {
    if (typeof y =='undefined') {
        return function (y) {
            return x + y;
        };
    }
    else {
        return x + y;
    }
}

var nextFn = currying(5);
console.log(nextFn(5));//10

Universal curry function

function currying(fn) {
    var slice = Array.prototype.slice;
   //Array the arguments object and exclude the first parameter fn
    var args = slice.call(arguments, 1);

    return function () {
        var newArgs = slice.call(arguments);

        return fn.apply(null, args.concat(newArgs));
    };
}
//test
function add(a, b, c, d, e) {
    return a + b + c + d + e;
}

//One step curry
var next = currying(add, 11, 22);
//Two-step curry
var next2 = currying(next, 33);
//Three-step curry
var next3 = currying(next2, 44);

next(33, 44, 55);//165
next2(44, 55);//165
next3(55);//165

What's the use of curry

Reference: http://www.zhangxinxu.com/wordpress/2013/02/js-currying/

1. Parameter reuse

After curry, it will return a new function, this function saves the repeated parameters through the closure.

2. Delayed execution

In fact, the Function.prototype.bindmethod in ES5 uses curried thinking

if (typeof Function.prototype.bind =='undefined') {
    Function.prototype.bind = function (o) {
        var me = this;
        var args = [].slice.call(arguments, 1);

        return function () {
            var newArgs = [].slice.call(arguments);

            return me.apply(o, args.concat(newArgs));
        };
    };
}

summary

After watching the curry, the first feeling is that it is so tall, but... I don't seem to feel any use for it at the moment. . .

In fact, I think there is a paragraph in Zhang Xinxu’s blog JS Currying that makes sense

Recently, I was reading the book "JavaScript Patterns", my goodness, the various design patterns (such as factory pattern, appearance pattern, observer pattern) that appeared in it can't be counted with one hand. Moreover, these terms are very abstract, and when the book is closed, the eyes are wide and small. It feels like that there are 10 black people in the room, and each black person has a name of "¥%#...¥". I can barely remember it. When the light is turned off, the room goes dark, and when the light is turned on again At that time, the name of every black man became..."hello".

In fact, these modes have been used more or less in actual use. When we see the concept of "xx mode", we will suddenly startled: "Oh, it turns out that this is called the'observer mode', etc.". The question to be discussed now is, do we need to remember all these "xx patterns" and understand their core counterparts?

This question is similar to that I can understand NBA basketball games. Do I need to remember all teams and their players? If you want to become a JS god, from this goal, this is necessary; for example, a good basketball commentator must know the name of each team, the players and even the surrounding gossip. However, reality is very important. If even the basic things related to JS functions can't be controlled well, obviously, just gnawing on these seemingly unintelligible concepts will only cause confusion. If you think you can go one step further, you should first understand a few familiar usage patterns that you are used to, enough to cope with the actual project; other concepts and so on are more just gimmicks, and the practicality is actually not great.

Just like the currying in this article, it looks very advanced and seems to be a bit useful. However, the flexibility of JS allows many implementations to completely get rid of the shackles of the concept of "currying" and implement them in a more easy-to-understand way. However, even if the practicality is not high, we still have to understand, because you don't know when you will use it. For example CSS in display:table;some cases can solve some thorny issues (secret!).

Therefore, the currying of this article requires at least a general idea...

var gitalk = new Gitalk({ clientID: '82f3fb6574634b43d203', clientSecret: '5790d9fdbfe9340d29810b694b3b2070a9a3b492', repo:'zfengqi.github.io', owner:'zfengqi', admin: ['zfengqi'], id: winMode: true .loc.pathname, }); gitalk.render('gitalk-container');

Reference: https://cloud.tencent.com/developer/article/1563214 Function curry (Haskell Curry)-Cloud + Community-Tencent Cloud