What is functional programming
Function Program is a programming paradigm or style, similar to object-oriented programming.
object-oriented
Abstract objects in the real world into classes and objects in the object-oriented world. throughencapsulation
.inheritance
andpolymorphism
To disguise the connection between things.- while
Functional programming
The real worldthings
withthings
Is abstracted into the program world- The essence of the program: according to the input through some operations to get the corresponding output, the program development process will involve a lot of input and output functions
- A function in functional programming is not a function (method) in a program, but a mapping in mathematics, such as y = sin(x), and the relation between x and y is a function
Pure functions
:The same input always leads to the same output
// Non-functional
let a = 1
let b = 2
let c = a + b
console.log(c)
/ / function type
function add(a,b){
return a + b
}
let c = add(1.2)
console.log(c)
Copy the code
The function is a first-class citizen
-
Functions can exist in variables
-
Functions can be arguments
-
A function can be a return value
-
Assign a function to a variable:
let func = function(){
console.log('first-class function')
}
func()
Copy the code
Higher-order functions
What is a higher-order function
Higher-order functions:
- You can pass a function as an argument to another function
- You can treat a function as the return result of another function
Function as argument:
function forEach(array,fn){
for(let i=0; i<array.length; i++){ fn(array[i]) } }function filter(array,fn){
let res = []
for(let i=0; i<array.length; i++){if(fn(array[i]))
{
res.push(array[i])
}
}
return res
}
Copy the code
The fn function is passed as an argument to forEach and filter, so forEach is a higher-order function.
Function as the return value:
function makeFunction(){
let msg = 'hello world'
return function(){
console.log(msg)
}
}
// the fn function is executed only once
function once(fn){
let done = false
return function (){
if(! done) { done =true
fn.apply(this.arguments)}}}// The above once function uses the closure to store 'done' in the closure, so no matter how many times once is called, fn will only execute once
Copy the code
closure
closure
: Functions are bundled with their surrounding states to form closures.- You can call an inner function of a function and access its inner members from another scope
function out(){
let msg = 'hello world'
return function(){
console.log(msg)
}
}
const fn = out()
fn() //hello world
Copy the code
Since the outer function is referenced externally, it is not destroyed after execution, and its internal variables can be saved
Currie,
Concept: To convert a function that takes multiple arguments to one that takes only some arguments, and then returns a function that takes the rest of the arguments, called the currization of the function
Function:
- You can make functions more flexible and less granular
- Functions of several variables can be transformed into unary functions, and more powerful functions can be produced by combination of functions
Implement a Currie function
function curry(fun){
let argLengs = fun.length
const fn = function(. args){
if(args.length >= argLengs){
return fun.apply(fun,args)
}
else{
return function(){
returnfn(... args.concat([].slice.call(arguments)))}}}return fn
}
function sum(a,b,c)
{
return a + b + c
}
const sumCurry = curry(sum)
let sumer = sumCurry(1) (2) (3)
console.log(sumer) / / 6
Copy the code
Above, Curry is a currified function, sumCurry is the currified sum function.
Combination function
- Take multiple functions as arguments and combine them into a new function
- Avoid onion code, which has multiple layers of nested function calls.
Data pipeline
- A pipe is a function. A piece of data X comes in through the pipe and y comes out the other side of the pipe.
- It is possible to split a long pipe (big function) into several short pipes (small function)
fn = compose(f1,f2,f3)
fn()
Copy the code
Function composition
If a function needs to be processed by multiple functions to get the result, you can combine the intermediate functions into a single function.
- Function combinations are executed from right to left by default
A simple combinatorial function:
function compose(f,g){
return function(x){
return f(g(x))
}
}
Copy the code
Implement a combinatorial function:
onst reverse = arr= > arr.reverse()
const first = arr= > arr[0]
const toUpper = s= > s.toUpperCase()
// combine functions
function compose(. args){
return function(value){
return args.reverse().reduce((count,fn) = > {
return fn(count)
},value)
}
}
}
function f2(){}
const a = compose(toUpper,first,reverse)
console.log(a(['asd'.'dsa'.'wqe'])) //WQE
Copy the code