If you are using JavaScript, you should know that there is functional programming in JavaScript, and first-class Functions in JavaScript.
Functional programming
Functional is a form of programming where you can pass functions as arguments to other functions and return them as values. In functional programming, we think and program in terms of functions.
Higher-order functions
JS functions actually refer to a variable
const test = function(a, b) {}
function test() {}
Copy the code
If arguments in a function can accept variables, a function can accept a function as a variable. A higher-order function can be understood as: a function that takes another function as a parameter is a higher-order function. There are many higher-order functions in JavaScript. Common higher-order functions are:
- setTimeout, setInterval
- Array ES6 extension methods (map, Reduce, filter, forEach, every, some).
function test(a, b, pow) { return pow(a, 2) + pow(b, 2); } function pow(x, n) { return Math.pow(x, n); Const ret = test(2, 3, pow); const ret = test(2, 3, pow);Copy the code
Higher order functions
- Abstracted into a completely separate function body
- The biggest feature of higher-order functions is that they are easy to expand and maintain
- The behavior of a method after execution is coded by the function parameters in higher-order functions
Curry – The idea of functional programming
Currization: Storing variables and methods in functions without destroying them; To transform a function that receives multiple parameters into a function that receives a single parameter (the single parameter is the first of the many).
function test(a, b, c) { return a + b + c; } // We want all of the following executions to have the same result: test(1, 2, 3); test(1)(2)(3); test(1, 2)(3); test(1)(2, 3);Copy the code
Now let’s start with some simple -> difficult examples
Function curry(fn) {const arg1 = array.from (arguments).slice(1); Return function(arg2) {const arg2 = arg1.concat(array.from (arguments)); Cancat (arg1, arg2) return fn. Apply (this, arg2); } } function add(a, b, c, d) { return a + b + c + d; } const add2 = curry(add, 1, 2); const ret = curry(3, 4); // The correct result is: 10Copy the code
The corrified function implemented above will give us the final result if we pass a particular parameter, but if we pass a total of three parameters, For example, (1,3)(4), this will return NaN(because we missed one argument, undefined + number = NaN when the add was added). On the other hand, we only return function once now, and we can’t write it by hand all the time, so we need recursion. The correct way to write it
function curry(fn, len) { len = len || fn.length; Const func = function() {const arg1 = array.from (arguments).slice(1); return function() { const arg2 = arg1.concat(Array.from(arguments)); return fn.apply(this, arg2); } } return function() { const argLen = arguments.length; If (argLen < len) {const formatedArr = [fn].concat(array.from (arguments)); return curry(func.apply(this, formatedArr), len - argLen); } else {fn return fn.apply(this, arguments); } } } const add1 = curry(add, 4); let ret = add1(1, 2, 3)(4); //ret = 10 let ret = add1(1)(2, 3, 4); //ret = 10Copy the code
** Key function parameter passing **