Functions are the most important part of our program, so let’s review how they work

What is a pure function

In simple terms, functions that do not depend on external arguments: e.g

	const num = 2;
    function Fn(arg){
    	return num*arg
    }
    // Fn is not a pure function
Copy the code
function Fn2(num,arg){
	return num*arg
}
// Fn2 is a pure function. You can use the ES6 arrow function to make the function look more concise

const Fn2 = (num,arg) = > num*arg
Copy the code

Pure functions are the basis for us to achieve higher-order functions, and we should try our best to write pure functions when we write daily functions

What is a higher-order function

The definition of a higher-order function is also simple: the argument is a function, or the function that returns a function, and the following functions are higher-order functions

	function Fn(fn){
    	fn()
    }
    
    function Fn2(){
    	return function(){}}Copy the code

The closure we often talk about is also a kind of higher-order function, such as:

	function Fn(){
    	const num = 1;
        function Fn2(){ 
        	console.log(num) / / 1
        }
        return Fn2
    }
	
Copy the code

In this case, Fn2 can be said to be a closure, and Fn can be said to be a higher-order function

Use higher-order functions to wrap map methods

Some of our common methods, such as map,every and some, are high order functions encapsulated by ES6. What if they are implemented internally? Let’s simulate implementing a map method

Array.prototype.myMap = function (fn, context) {
  let arr = this;
  let temp = [];
  for (let i = 0; i < arr.length; i++) {
    let result = fn.call(context, arr[i], i, arr)
    temp.push(result)
  }
  return temp
}
Copy the code

test

const arr = [1.2.3.4]
const arr2 =  arr.myMap(item= > {
 	return item + 1
})
/ / output 5-tetrafluorobenzoic [2]
Copy the code

Use higher-order functions to encapsulate the once method

When we need functions that are executed only once, we can use higher-order functions, such as:

function once(fn) {
  let flag = true;
  return function () {
    if (flag) {
      fn()
      flag = false
    } else {
      console.log('It's already been done.')}}}function dome() {
  console.log('I'm going to execute.')}const d = once(dome)
d()
d()
/* I want to execute already executed */
Copy the code

The function is currified

Currization is the process of transforming a multi-parameter function into a nested unary function. Functionally, it achieves the refinement of function functions. Let’s start by simply implementing a Currization function

	let Fn = (x,y) = >x+y
    // use the Fn function
    Fn(1.2) / / 3
Copy the code

Let’s take f sub n and transform it

let Fn = (x, y) = > x + y;
const curry = (fn) = > {
  return (x) = > {
    return (y) = > {
      return fn(x, y);
    };
  };
};
const fn = curry(Fn);
fn(1) (2);  / / 3
Copy the code

As can be seen, when we use fn function, we can pass parameters individually, which is more beneficial to the decoupling of function. Let’s encapsulate a general Coriolization function, so that no matter how many parameters we have, we can turn them into a single parameter

const curry = function (fn) {
  return function curriedFn(. args) {
    if (args.length < fn.length) {
      return function () {
        returncurriedFn(... args.concat([...arguments])); }; }return fn(...args);
  }
}
Copy the code

We use curry

const fn = (x, y, z, a) = > x + y + z + a;
const myfn = curry(fn);
console.log(myfn(1) (2) (3) (1)); / / 7
Copy the code

The curry function is implemented by gathering arguments and then executing them

conclusion

Function is the soul of programming language, high order functions need us to appreciate the charm of programming

The last

Keep an eye on wechat public numbers. Make friends