preface
Functional Programming (FP) is a Programming paradigm. From a macro perspective, it abstracts the relationship between things as a function, and the function here is a function in the exponential sense, which represents a relationship. Let’s say we have a variable x. I’m going to get some value y by some connection to f. This relationship can be abstracted as y = f(x). Back in our programming world, a program is essentially a function of input and output, and we abstract different operations into functions, which can be combined to generate more complex functions.
Other Programming paradigms
- Process-oriented: Implement a program by following a set of steps.
- Object-oriented: Abstracts things into classes and objects in programs, demonstrating relationships between things through encapsulation, inheritance, and polymorphism.
Functions are first-class citizens
- Functions can be stored in variables
- Functions can be taken as arguments
- Function as the return value
Functions in JavaScript:
- Is a normal object
- Create Function object: new Function()
Higher-order functions
A function that returns another function is called a higher-order function. Features:
- You can pass a function as an argument to another function.
- You can treat a function as the return result of another function.
Higher order functions commonly used in arrays
ForEach, map, filter, every, some, find/findIndex, reduce, sort…
Meaning of using higher-order functions
- Solve general problems in an abstract way
Commonly used higher-order function implementation
- forEach
function forEach(array, fn) {
for(let item of array) {
fn(item)
}
}
Copy the code
- map
const map = (array, fn) = > {
let results = [];
for (let value of array) {
results.push(fn(value));
}
return results;
};
Copy the code
- filter
const filter = (array, fn) = > {
let results = [];
for (let item of array) {
if(fn(item)) { results.push(item); }}return results;
};
Copy the code
- every
const every = (array, fn) = > {
let result = true;
for (let value of array) {
result = fn(value);
if(! result) {break; }}return result;
};
Copy the code
- some
const some = (array, fn) = > {
let result = false;
for (let value of array) {
result = fn(value);
if (result) {
break; }}return result;
};
Copy the code
closure
A function is bound with a reference to its surrounding state (lexical environment), or the function is surrounded by references. Such a combination is called a closure. That is, closures allow you to access the scope of an inner function from within its outer function. – MDN
Use skills
The basic repeatable logic serves as the external function, and the operational logic part of the variation serves as the internal function.
function makePower(pow) {
// An internal anonymous function accesses a POW outside the function, forming a closure.
return function (number) {
return Math.pow(number, pow);
};
}
// Square/cubed
let power2 = makePower(2);
let power3 = makePower(3);
console.log(power2(3));
console.log(power3(3));
Copy the code
Pure functions
The same input always gives the same output. There are no side effects. It doesn’t hold the intermediate results, it’s stateless. Does not depend on external variables or states.
advantages
Support for parallel processing, where functions operating in parallel on shared memory data in a multi-threaded environment are likely to cause accidents. Pure functions, on the other hand, do not need to access shared memory data, so they can be run as they wish in a parallel environment.
Side effects
The side effect is that the function becomes impure. Pure functions return the same output based on the same input. If the function is dependent on an external state and cannot guarantee the same output, this can cause side effects.
Currie,
When a function has more than one argument, it is called by passing some of the arguments (which never change), and then returns a new function that takes the rest of the arguments and returns the result.
advantages
- Let’s pass a function fewer arguments and get a new function that has some fixed arguments memorized (cache function arguments)
- Makes functions more flexible and less granular
- Functions of several variables can be converted into functions of one variable, and functions can be combined to produce powerful functions
Implement a Curryized function
function curry(func) {
return function curriedFn(. args) {
// Determine the number of arguments and parameters
if (args.length < func.length) {
return function () {
returncurriedFn(... args.concat(Array.from(arguments)));
};
}
returnfunc(... args); }; }function getSum(a, b, c) {
return a + b + c;
}
const curried = curry(getSum);
console.log(curried(1.2.3));
console.log(curried(1) (2.3));
console.log(curried(1) (2) (3));
Copy the code