This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Functional programming
Architectural Design Another programming paradigm, functional programming, is concerned with the mapping between data, which abstracts the computation process as an expression evaluation. Let’s look at the following function code to convert an array to an array object:
Function AryToObjectAry() {const ary = ["jasen-yang", "Tom-han "," marri-han ", "lucy-any"]; const newObj = []; ary.forEach((value) => { let names = value.split("-"); let newNames = []; names.forEach((name) => { const temp = name[0].toUpperCase() + name.slice(1); newNames.push(temp); }); newObj.push({ name: newNames.join(" ") }); }); console.log(newObj); return newObj; } (()=>{ AryToObjectAry() })(); [{name: 'Jasen Yang'}, {name: 'Tom Han'}, {name: 'Marry Han'}, {name: 'Lucy Any'}]Copy the code
Define variables, loop through arrays, capitalize values, each function does its job, and follow certain steps from function input to output. It’s pretty clear what’s going on when you read the code, but when something goes wrong it’s hard to locate, and there’s a lot of logic and variables involved.
What if every procedure was a function? Take a look at the following pseudocode:
function convertAry() {
const ary = ["jasen-yang", "tom-han", "marry-han", "lucy-any"];
const getNames = (value) => {
return value.split("-");
};
const toUpper = (value) => {
return value[0].toUpperCase() + value.slice(1);
};
const convertName = (newObj,value) => {
return newObj.push({ name: value });
};
const newObj = [];
ary.forEach((value)=>{
const newName = compose(getNames(value),toUpper);
const result = compose(newObj,convertName(newName));
});
return newObj;
}
Copy the code
Looking at the whole programming idea, we can clearly see that functional programming focuses on functions rather than processes, and solves problems through the combination and transformation of functions. Each function can see what it is doing at a glance, and functions make the code more semantic and readable.
Functional programming focuses on building relationships, rather than focusing on the data between functions, by building an efficient pipeline that solves all problems at once.
Functional programming features
Functional first-class citizen
In functional programming, variables cannot be modified. All variables can be assigned only once, and all values are handled by passing arguments. Functions, as first-class citizens, can be defined anywhere, as arguments and return values, and functions can be combined.
Stateless and immutable data
The heart of functional programming
- Data is immutable. If you modify an object, create a new object instead of modifying an existing object
- Stateless, a function that outputs the same result on a given input whenever it is run, completely independent of changes in external state
Inert to perform
Functions are executed only as needed, without generating meaningless intermediate variables. For example, you write functions all the way through the process and only produce the actual results at the end.
The resources
- Mp.weixin.qq.com/s/ezHCBGr6S…
- Mp.weixin.qq.com/s/BzMHd4KNb…
\