Why and what is functional programming
- Functional programming has received increasing attention with the popularity of React
- Vue3 is also embracing functional programming
- Functional programming can discard this
- Better use of Tree Shaking to filter out useless code during packaging
- There are many libraries that can help us with functional development: Lodash, underscore
Functional Programming (FP),FP is one of the Programming paradigms, we often hear of procedural Programming, object-oriented Programming
- The way of thinking of object-oriented programming: abstract the things in the real world into classes and objects in the program world, and demonstrate the connection of things and events through encapsulation, inheritance and polymorphism
- Functional programming way of thinking: Abstracting things and their relationships from the real world to the program world
- The essence of the program: according to the input through some operation to obtain the corresponding output, the program development process will involve many functions with input and output
- X ->f ->y,y=f(x)
- A function in functional programming is not a function (method) in a program, but a function (mapping) in mathematics
- The same input always produces the same output (pure function)
- Functional programming is used to describe mappings between data (functions)
Features of functional programming (pure functions, Currization, function combinations, etc.)
- Functional first-class citizen
- Functions can be stored in variables
- Function as argument
- Function as the return value
In JacaScript a Function is a normal object (via new Function()), we can store the Function in a variable/array, and it can be used as an argument and return value of another Function, We can even construct a new Function while the program is running by using new Function('alert(1)')
- Higher-order functions
- Implementation of the once function (application of the function as a return value) the function is executed only once
- Array methods (application of functions as arguments)
- Use the meaning of abstract functions
- Abstraction can help us mask the details and focus only on our goals
- Higher-order functions are used to abstract general problems
- closure
- A function is bundled with references to its surrounding state (lexical environment) to form a closure
- You can call an inner function of a function from another scope and access members of that function’s scope
- The essence of closures: functions are placed on an execution stack at execution time and removed from the stack when the function completes execution, but scoped members on the heap cannot be freed because of external references, so internal functions can still access the members of the external function
Application scenarios of functional programming Functional programming Lodash
-
Pure functions
- Pure functions: The same input always yields the same output without any observable side effects
- A pure function is like a function in mathematics,y=f(x).
- Lodash is a library of pure functions that provides methods for operating on arrays, numbers, objects, strings, functions, and more
- Arrays of slice and splice are pure and impure functions, respectively
- Slice returns the specified portion of the array without altering the original array
- Splice operates on an array and returns that array, changing the original array
- Application :(fine-grained functions), we can hand over the execution results of one function to another function to process
- Benefits:
- cacheable
- Because the same input of a pure function object always has the same result, the result of a pure function can be cached again
- testable
- Functions make testing easier
- Parallel processing
- Parallel manipulation of shared memory data in a multithreaded environment is likely to cause unexpected problems
- Pure functions do not need access to shared memory data, so they can run at will in parallel (Web workers)
- cacheable
- Side effects: Side effects make a function impure. Pure functions return the same output based on the same input. Side effects can occur if the function is dependent on external states that cannot guarantee the same output
- Sources of side effects:
- The configuration file
- The database
- Get user input
- .
All external interactions are possible proxy side effects, which also reduce the generality of the method and make it unsuitable for expansion and reuse. At the same time, side effects will bring security risks to the program and uncertainty to the program. However, side effects cannot be completely banned, and they should be controlled as much as possible
- Pure functions: The same input always yields the same output without any observable side effects
-
Currie,
- When a function has more than one argument, it is called by passing some of the arguments (these arguments never change).
- It then returns a new function that takes the remaining arguments and returns the result
function curry(func){ return function curriedFn(... If (args. Length < func.length){return function(){return curriedFn(... args.concat(Array.from(arguments))) } } return func(... args) } }Copy the code
-
Function composition
- Pure functions and Currization are easy to write onion code h(g(f(x)).
- Function composition allows us to take fine-grained functions and recombine them to create a new function
- Function combination: If a function needs to be processed by multiple functions to get the final value, it is possible to combine the intermediate functions into a single function
-
A function is like a pipeline of data, and a combination of functions connects these pipes, allowing data to pass through multiple pipes to form the final result
-
Function combinations are executed from right to left by default
-
The combination of functions should satisfy the associative law (we can combine g and H, or f and g, and the result is the same).
-
Lodash/FP LoDash’s FP module provides a functional programming friendly approach to use
-