Like the Fibonacci sequence, I was stunned when I first heard the Corrification

Reference for this article:

  1. JavaScript design patterns and development practices

  2. Currying in JavaScript

  3. Curried JavaScript functions

preface

The purpose of this article is to let you simply understand the Curryization and anti-Curryization, here is not to do in-depth exploration, only to lead you to pack force good, read still do not understand you cut me.

Let’s take a quick look at what they do.

Currization is also called partial evaluation, which literally means not evaluating immediately, but evaluating when needed. If you’re confused, read the whole article and then go back and look at it.

So what the anti-Cremation does is, when we call a method, we don’t have to worry about whether or not the object has the method when it was designed, as long as the method applies to it, we can use it on the object.

Currie (curring)

We have a scenario where we log the total amount of overtime a programmer does in a month. Ok, so what we do first is we log the amount of overtime a programmer does in a day, and then we add up all the overtime hours in a day for a month to get the total amount of overtime in a month.

But here’s the thing: There are many ways to do it, like the simplest:

var monthTime = 0;

function overtime(time) {
 return monthTime += time;
}

overtime(3.5);    The first day / /
overtime(4.5);    / / the next day
overtime(2.1);    / / on the third day
/ /...

console.log(monthTime);    / / 10.1Copy the code

It’s fine to add up each incoming overtime, but you know, if there’s a lot of data, it’s a big performance sacrifice.

So what? That’s the problem that Cremation is trying to solve.

In fact, we don’t need to calculate the overtime hours every day, we just need to save the overtime hours every day and calculate the total overtime hours of the month at the end of the month. Therefore, we only need to calculate the overtime hours once at the end of the month.

The following function is not a complete implementation of the Currization function, but it can help us understand the main idea:

var overtime = (function() {
  var args = [];

  return function() {
    if(arguments.length === 0) {
      var time = 0;
      for (var i = 0, l = args.length; i < l; i++) {
        time += args[i];
      }
      return time;
    }else {
      [].push.apply(args, arguments); }}}) (); overtime(3.5);    The first day / /
overtime(4.5);    / / the next day
overtime(2.1);    / / on the third day
/ /...

console.log( overtime() );    / / 10.1Copy the code

So that’s the whole idea, and I’m sure you get the idea, but there’s a lot of them on the Internet, so you can Google them.

Uncurring

The role of anti-Curryification has been described in the prologue, and here is where it comes from.

In 2011, Brendan Eich, the father of JavaScript, tweeted about the idea of anti-Currization, and this code is one of the ways to do it:

Function.prototype.uncurring = function() {
  var self = this;
  return function() {
    var obj = Array.prototype.shift.call(arguments);
    return self.apply(obj, arguments);
  };
};Copy the code

Let’s take a look at what this code does.

To convert the array.prototype. push method to a generic push function, just do this:

var push = Array.prototype.push.uncurring();

// Test it
(function() {
  push(arguments.4);
  console.log(arguments); / / [1, 2, 3, 4]}) (1.2.3)Copy the code

The arguments could have been no push method, usually, we all need to use an Array. The prototype. Push. Call to realize the push method, but for now, direct call push function, intention is concise and clear.

As in the prologue, we don’t have to worry about whether the object owns the method, as long as it applies to the method, it can be used (similar to the duck type).

Let us analysis the call Array. Prototype. Push. Uncurring () the code, what happened:

Function.prototype.uncurring = function() {
  var self = this;  //self is array.prototype.push

  return function() {
    var obj = Array.prototype.shift.call(arguments);
    / / obj is {
    // "length": 1,
    / / "0" : 1
    / /}
    // Arguments' first object is truncated (i.e. the push object), leaving [2]

    return self.apply(obj, arguments);
    / / equivalent to the Array. The prototype. Push. Apply (obj, 2);
  };
};

// Test it
var push = Array.prototype.push.uncurring();
var obj = {
  "length": 1."0" : 1
};

push(obj, 2);
console.log( obj ); //{0: 1,1: 2, length: 2}Copy the code

Here you should have a preliminary understanding of collerization and anti-Collerization, but in order to skillfully use in development, we still need to understand their inner meaning in a deeper way.

Friends who like this article can follow my wechat public number and push some good articles irregularly.

This article is from Rockjins Blog, please contact the author for reprinting. Otherwise, legal liability will be investigated.