To be clear, functional programming here does not refer to the act of writing and calling functions. Second, here we explain why we should embrace functional programming. In business, our programming ideas tend to be process oriented, and such code is often highly aligned with the business scenario, which often makes maintenance difficult, such as when refactoring 😇.

In large amounts of business code there are functions that can be extracted as common steps. In this case, there will often be such a behavior, we extracted these public steps, and then call the function in the business code, and then arbitrarily add their own parameters according to the business scenario (I often do before), will find that the draw is equal to the white draw 🤯

So, here’s the idea of functional programming

Functional programming requires functions to be “first class citizens,” that is, functions that can be passed like variables as arguments to functions. This kind of thinking in JavaScript is a perfect fit (personally), because JS object orientation is a long story, this is also 🔥 burial ground, so this kind of functional coding ideas for our usual code organization has a certain help.

So what are the practical principles of functional programming? Functions represent the transformation of relationships, i.e. the output of input values to output values. In front end practice, the relevant operation is to convert the source data into the data we want. The function we call describes the transformation process, in which we can break our previous business code into several steps that are independent pure functions (independent of external state, with no reference to parameters). These functions are then combined with the business code to fit the business scenario.

The two diagrams above represent the difference between our approach to a process-oriented and functional programming approach to a business scenario. Functional programming allows us to assemble these pure functions like building blocks. Let’s do a simple example

const compose=(... fn)=>(... ReduceRight ((val,fn)=>fn.apply(null,[].concat(args)),args) : compose

const pick=(obj,list)=>Object.keys(obj).reduce((obj,val)=>list.includes(val)? obj:{... obj,[val]:obj[val]},{}``)

const check=(obj)=>Object.keys(obj).findIndex((val)=>list.includes(val))

compose(check,pick)(obj,list)

In view of my limited level (just entered the society to accept severe beating), there may be some wrong place to write, if there is wrong, please point out. Thanks for reading 😘