preface

Functional programming has been hot in recent years. In fact, functional programming has existed for a long time. There are well-known languages that support this paradigm, such as C, JavaScript, PHP, etc.

What is functional programming

First, let’s understand what functional programming is:

Functional programming, or functional programming, is a programming paradigm that treats computer operations as functions and eschews the use of program states and mutable objects. The calculus of λ is the most important foundation of the language. Furthermore, the function of the λ calculus can accept functions as input parameters and output return values. “– Wikipedia

Function is a programming paradigm, and function is also a programming thinking mode with function as the core.

Functions are first-class citizens

Functions assign values to variables that can be passed as arguments or returned as values.

// Functions can be assigned to variables
const foo = function() { return "foo" }
// Functions can be passed as arguments and can be returned as values
function bar(foo,callback){
	const result = foo()
  return callback(result)
}
bar(result= >{
	console.log(result)
})
Copy the code

Pure functions & higher order functions

1. Pure functions

A pure function is an important concept in functional programming. Simply put, a function whose return depends only on its arguments and has no side effects during execution is called a pure function.

  • Avoid side effects
  • reusability

2. Higher-order functions

Functions that are input or output by functions are called higher-order functions.

The function input parameter is the function

The output value of a function is a function

closure

function foo(name){
	return function(){
    return "hello," + name
  }
}
Copy the code

Three, function of the correct posture

1. Avoid iterations

Avoid for, while, do… While, the figure above shows how to make a sandwich in a functional way. The first step is to slice and chop the ingredients, the second step is to add and combine the ingredients, and you have a sandwich. So the conversion to imperative is to do everything in one iteration.

2. Avoid data variation

Four, advantages and disadvantages & use scenarios

1. The advantages and disadvantages of

  • advantages
    • Easy to write and read
    • Reliability (pure function)
    • Lazy evaluation (higher order functions)
    • Easy to test
    • Combination development
    • Easy to “concurrent programming”?
  • disadvantages
    • Parts are suitable for performance sacrifice
    • Memory consumption?

2. Application scenario

  • Practice in everyday code: Handle complex, multiple chain operations on data.

  • Mocha: Unit test library

  • Redux: React based tool library
  • Lodash
  • Mirror

Q & A

1. FP vs OOP

  • The core
    • FP: Solve problems with functions as the core
    • OOP: Solve problems with classes and objects. Features: abstraction, encapsulation, inheritance, polymorphism
  • Dynamic, extension mechanism
    • FP: combination + higher order function
    • OOP: Inheritance + polymorphism

2. FP vs IP

Compared with Imperative Programming (Imperative Programming), functional programming puts more emphasis on the results of program execution rather than the process of execution. It advocates the use of several simple execution units to make the calculation results gradually and deduce complex operations layer by layer, rather than designing a complex execution process. “– Wikipedia

imperative

const arr = ["zhangsan"."lisi"."wangwu"]
for(let i = 0; i < arr.length; i++){
    if(arr[i]==="lisi"){
       arr.splice(i,1)}}console.log(arr)
Copy the code

functional

const arr = ["zhangsan"."lisi"."wangwu"]
const result = arr.filter(x= >x! = ="lisi")
console.log(result)
Copy the code

Reference & Recommendation

  • Functional introduction video: www.youtube.com/watch?v=e-5…
  • Functional advantages and disadvantages: blog.csdn.net/HXCURTAIN/a…
  • Functional programming: www.cnblogs.com/fs0196/p/12…
  • Higher-order functions: zhuanlan.zhihu.com/p/49579052
  • JavaScript is a functional-led tool library
    • lodash
    • ramda
    • mirror