Functional programming
Functional programming concepts
Functional Programming (FP), one of the Programming paradigms, similar paradigms: object-oriented Programming, procedural Programming
Note: A function in functional programming is not a function (method) in a program, but a function mapping in mathematics
Way of thinking:
- Object-oriented programming:
Things in the real world are abstracted into classes and objects in the program world, and the relations between things are expressed through encapsulation, inheritance and polymorphismCopy the code
- Functional programming
The abstraction of things in the real world and the connections between things into the programming world is the abstraction of the computing processCopy the code
Question: is functional programming an abstraction of an operation equivalent to or similar to procedural programming?
Solution: Process-oriented programming is to analyze the steps needed to solve the problem, and then use the function step by step to achieve, use the call in turn, there are variable state and mutual dependence, belong to serial; Functional programming is more mathematical, implementation-focused, stateless, side-effects free, and therefore not equivalentCopy the code
Functional programming features
- Pure functions
- The same input always gives the same output
- Reusable
The function isFirst class citizens
- Functions can be stored in variables
- Functions can be taken as arguments
- Functions can be returned as values
Higher Order function
A function can be passed as an argument to another function (to make it more flexible) and a function can be returned
Meaning of higher order functions
Abstractions can mask the details and focus only on what is relevant to the goal, and higher-order functions are a way to abstract a general purpose problem
Higher order functions are often used
forEach/filter/map/every/some…
- Every: checks each element in the array in turn, returning false if one is not met, and no further elements are checked
- Every implementation:
const every = (array, fn) = > {
let flag = true;
for (let value of array) {
flag = fn(value);
if(! flag) {break; }}return flag;
}
Copy the code
- The some function checks each element in the array in turn, returning true if one of the elements satisfies the specified condition, false otherwise, and no further elements are checked
- Some implementation:
const some = (array, fn) = > {
let flag = false;
for (let value of array) {
flag = fn(value);
if (flag) {
break; }}return flag;
}
Copy the code
closure
When a function is executed, it will be placed on an execution stack. When the function is finished, it will be removed from the execution stack. However, the scope members on the heap can not be released by external references, so the mechanism by which internal functions can still access external function members is closure;
In common sense, closures allow you to access the scope of an inner function from its outer function. When the outer execution returns the inner function, the inner and outer scopes are not released because of the reference relationship.
Question: When the external function is executed after the variable received, there is a variable to the function reference relationship, can not be released recycling; But if only execute, do not use variables to receive, is there a reference relationship? Will it be released for recycling as a whole? Is the scope member on the heap a reference from the variable to the object?
Answer: 1, closure occurs internally, and closure of cache related content, such as variables, functions, etc. The rest will not relevant content according to the browser to recycle and 2 GC mechanism has nothing to do, and if there is a variable storage, when the function is invoked, the inner function can access the outer function scope, and visited some attributes, constitute a closure at this time, if not there access and reference, Then only nested or higher-order functions are executed, and there are no closuresCopy the code
Pure functions
The same input always yields the same output without any observable side effects;
Functional programming does not preserve the intermediate results of calculation, so variables are immutable
The benefits of pure functions
- cacheable
- testable
- Parallel processing (Es6 + Added Web Worker, multi-threaded processing)
Side effects
If a pure function depends on an external state, the same output cannot be guaranteed and side effects can occur
Source of side effects
- The configuration file
- The database
- User input
Currie,
When a function has more than one argument, it is called with some arguments passed (which will not change in the future), and then returns a new function that takes the remaining arguments and returns.
Conclusion:
1. Currying can be done by passing fewer arguments to a function to get a new function with certain fixed arguments in mind (this is a kind of caching of function arguments, closures).
2. Make the function more flexible and make the function less granular
3. Can convert multivariate functions into unary functions, and can combine functions to produce powerful functions
Simulation of currying principle
function curry(fn) {
return function newCurry (. args) {
if (args.length < fn.length) {
return function() {
returnnewCurry(... args.concat(Array.from(arguments)))}}returnfn(... args) } }Copy the code
Bonus points
-
The delete operator, which deletes a property of an object, will eventually be released if there is no reference to the property
-
The delete operator returns true in all cases unless the property is a self-contained, non-configurable property, in which case it returns false in non-strict mode and raises a TypeError in strict mode
Note: If an object has a property in the stereotype chain with the same name as the property to be deleted, then the object will use that property in the stereotype chain after the property is deleted. 3. Any property declared using var cannot be deleted from the global scope or function scope. The delete operation cannot delete any function in global scope. 4. Any property declared with let or const cannot be deleted from the scope in which it is declared. This means that properties of built-in objects like Math, Array, and Object, as well as properties that have been set unsetable using the object.definedProperty () method, cannot be removedCopy the code