A coritization summary of js function

1. What is the Coritization of Functions?
Let's first look at what is defined in Wikipedia [1]: In computer science, Currying, or Carrierization, is a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns the function that accepts the remaining parameters. And return the result of the new function technology.

2. The function of the Coritization function:

(1) The reusability of function parameters can be achieved.

(2) Variable parameters can be fixed;

(3) The calculation of the delay function can be delayed.

The coritization function is abstract in understanding and needs strong js primitive basis.

Let's start with the theme:
The function coritization must be made clear that it is a coritization of a known function (equivalent to processing a known function to achieve the effect of coritization).
Look at a piece of code first.

function add (x,y) {
	return x+y

function curryadd (x) {
	return function (y) {
		return x + y

The above code implements the idea of curryadd function, which is transformed into curryadd function. Curryadd returns a function that accepts the parameter y and achieves the same result as curryadd (1) (2) and add (1, 2). Change the acceptance of multiple parameters to acceptance of a single parameter.

But what if you wear five parameters, as described above? How about six? Is it not necessary to nest layer by layer, just like we wrote asynchronous callbacks before we did not have promise s, into the nightmare of function nesting?

So let's make a change, wrap it up, write a factory function, let this factory function, specially wrap functions like add, and make it curricula (here's the idea, function curricula, that is, we wrap a function so that it can be curriculated)
The following functions are a little complicated to understand for the first time, but we parse them step by step.

//Here we want to create an add function, and all we need to do is to coritize the add function.
//The effect is that we get the same results whether we curryadd (a, b) (c) or curryadd (a) (b) (c) as above.
function add (a,b,c){
//First of all, the curry function is created. The parameter it must accept is that it wants to be curried into a function.
function curry(fn){
	//Understand here that fn.length is the parameter that gets accepted by the incoming FN function, such as len=3 when add s are passed in.
	//fn.length grammar does not understand to consolidate the foundation of js
	var len = fn.length;
	//Here our agrs saves the parameters passed in by the factory function except fn, which is the concept of closures.
	//Explain, because argument ation is not an array, but an array of classes has no slice method.
	//So to use Array.prototype.slice.call() to turn around, or this does not understand a look at the js Foundation
	var agrs = Array.prototype.slice.call(arguments,1);
	//The return value of curry function is a function, because according to our purpose, we want it to be called (), which is the concept of higher-order function.
	return function () {
		//_ args is a parameter that holds the function returned by our () call
		var _args = Array.prototype.slice.call(arguments);
		//Here we combine the parameters passed at the time of creation and the parameters passed at the time of invocation into an array.
		var newargs = _args.concat(agrs);
		//Compare the length of the spliced array with the length of the parameters of the incoming function that we are going to coriify.
			//What this means is that there are parameters that have not been passed. Without calculating the results, only the parameters that have been passed are saved in memory.
			var fnargs =[fn].concat(newargs);
			//Recursive calls to curry factory functions
			return curry.apply(this,fnargs)
			//If the parameters are enough, we call the fn function directly to calculate the result.

Here we have completed the Corey Chemical Plant function.

Then one thing will come to mind. What scene will this Corey turn his mind upside down?
What do the first few functions of the article mean? Let's look at them one by one.
(1) The reusability of function parameters
For example, we have a function like this.

function add(a,b,c,d,e,f,g){
	return a+b+c+d+e+f+g

If we call the add function many times, but the parameters of a-f remain unchanged, each time only g parameters become, if not coritization, then we have to pass all parameters once every time. Is it very troublesome? If coritization is used, that's all we need to do.

	//Call the curry we encapsulated above
	const curryadd = curry(add,1,3,5,7,8,9)
	//Can we just change the g parameter? Imagine if a-f is a very complex function of parameters.
	//Every time the caller knocks on the parameters, will he go crazy? Is it much easier to have Curitization?

(2) (3) Function, you try to use the idea of Coritization to realize the function of the bind() method can be comprehended.
The bind method is to bind the parameters to the function first, and fix this which is not changeable when the function is called.

Posted by sledge4 on Thu, 05 Sep 2019 22:52:47 -0700