Functional programming thought I understood

  Before understanding the functional programming thinking, we should first understand the functional programming originator lamda calculus.

  lamda Church of calculus is introduced in the 1930s was determined row in order to solve the problem, namely: whether all algorithms have the solution?

  In the process of solving this problem, the Turing and Church were to answer this question from two different angles:

  Turing step by simulating human problem solving, and created a Turing machine, then use the unlimited problem-solving capacity of the machine to get the answer, and the Church is another way to define the algorithm Church, then the Church has created a kingdom, that Kingdom there is only one element, that is, algorithms, and the Church in which this kingdom also introduced mathematics, mathematics numbers and arithmetic operators all use the Church to redefine it again (of course, not just defined by the Church because most understand mathematics principles of nature, so it can create your own math), now with digital, operators have, naturally can answer the question of whether all the algorithms have the solution.

  Here only briefly about, lamda calculus definition of 0, before learning lamda calculus, for I know is no zero, nothing, in lamda calculus definition of 0 is not applied any force, intact, 1 defined as a force is applied, definition 2 is applied twice the force, I do not know if my understanding is correct, in lamda calculus element is the smallest unit of algorithms, data to be processed is part of, is that the definition 0 in this algorithm is not only the world of the algorithm, the algorithm not only did this not only in line with the definition of 0, and the algorithm is no algorithm input data, or return to the original data.

 

  The above simply write a Turing and Church of example, just because of his grasp of these only superficial, but lamda calculus Church in a part of the final is to use lamda item to redefine mathematics, this approach does make people suddenly see the light because if everything is arithmetic, then, a matter to reach from one state to another, but is the result of conversion of numerous algorithms it, he and Turing essentially different, more similar thinking Turing machine and the human mind because you first need to program the Turing machine, every human problem-solving steps would have written, then the input parameters, return results, and lamda then abandoned this solution, but to start directly from the place of the most essential : convert all states but operation of the algorithm.

  That functional programming thinking and lamad calculus in which place is more like it? I think because lamda officially abandoned the steps defined in the act, so that the time of writing we conduct any business logic, are not necessary to write a new service processing function, but the use of already existing function to do a simple combination, you can get As a result, the benefits of doing so is much of the code reuse. for example:

  First day: we meet a demand, the teacher's name all the teacher's name is included in the notes plus all the word

  Ordinary thinking is:

  

addNameMark(teaList) {

  for (let i = 0; i < teaList.length; i++) {

    if (teaList[i].indexOf('于') > -1) {
        teaList[i] =teaList[i] + '备注';
    }

  }  

}

  Functional thinking is:

addNameMark(teaList) {

    teaList.map((item) => {
        item = item.indexOf('于') > -1 ? (item + '备注') : item;
    })

}

 

  What difference there is it? Compare the two codes, the processing logic function of thinking, is divided into two algorithms, the first algorithm is a treatment cycle, the second is note-processing algorithm, while the general concept of thinking is no algorithm, simply the task only, Imagine, as demand continues to iterate back as long as the need to use the local loop, we can use the map, map compared to the for loop, reducing the amount of code, while semantic, and now just needs a can not see, but the shares is the amount of code thousands of lines of code that whether the objective of saving it? Second: By some processing logic, to abstract to be assembled numerous algorithms, the benefits of doing so is split into separate algorithm can be reused in the future, and the code written in ordinary thinking is difficult to get multiplexing.

 

  So what is the core functional programming is it? That functional programming is the function of the atomic world, there is no smaller than his stuff, and we must use our abstract thinking for a handler to the abstract into multiple algorithm combination, and then the end result is not an ordinary problem solving that species written, but as data in the same order as the shuttle walking inside the pipe in a plurality of functions, the finally obtained the final result, the key here is that if a section of the abstract service code to assembly into a plurality of algorithms? Here's a suggestion that an algorithm handles only one thing, if you find that some of your algorithm processing inside the two things, you should try to split, and then how to define one thing or two things, different people understanding is not the same, there is a method: verb, to find your things inside the algorithm, after the verb exclude logic for judging if multiple verbs, and then deal with a number of things.

Guess you like

Origin www.cnblogs.com/mrzhu/p/12396968.html