Author: Li Yingjie, member of Meituan Financial Front End Team. Welcome to discuss FP together

Off topic: just talk about personal understanding of functional programming, welcome to discuss together. I’m not going to talk about higher-order functions and categorization, just some very basic questions. The advantages of functional programming will not be explained here, but there are several articles that I would recommend that you read.

The italic gray part is some personal ridicule and private goods

directory

  1. Why is functional programming having a Renaissance at the front end?

  2. What is functional programming?

  3. How does functional programming think?

  4. What are the differences between functional and object-oriented programming, and what are the pros and cons of each?

  5. Are Map and Reduce functional programming?

  6. recommended


Book connected to wen

4. Using this simple word problem, let’s try to compare functional programming with object-oriented programming.

In fact, this word problem gives some hints, so that we unknowingly walked into the “trap”

  • Mark this as a word problem and guide you to think mathematically
  • Given the known conditions, further guide you to use four operations to think

Remove the two hints, let’s take a look at this problem. Light rain went out to play with n apples, passing by Uncle Xue’s house, Uncle Xue gave her n apples, Aunt Xue gave her an apple. Passing by a brick home, light rain gave a brick n apples light rain now how many apples?

How to solve this problem with object-oriented thinking? The first step is to abstract the class and the second step is to add properties and methods

class Person {
  constructor(name, apples) {
    this.name = name;
    this.apples = apples;
  }

  receive(num) {
    this.apples += num;
  }

  give(num) {
    this.apples -= num;
  }

  getApples() {
    console.log(this.apples); }}let n = 10;
let xiaoYu = new Person('rain', n);
xiaoYu.receive(n);// Uncle Xue gave n
xiaoYu.receive(1);// Aunt Xue gave n
xiaoYu.give(n);// Give a brick n
let result = xiaoYu.getApples();/ / the answerCopy the code

Compare that to the functional thinking code

let n = 10;
/ / add
function add(x, y) {
  return x + y;
}
/ / subtraction
function sub(x, y) {
  return x - y;
}
let result = add(n, 1);/ / the answerCopy the code

Let’s start with the advantages of functional thinking code

  • The code is cleaner and has fewer lines
  • More efficient execution
  • Testing is easier. If we want to test the action of sending an apple, we only need to test the function sub. But for object-oriented code, we need to new an object, initialize name and apples, and run the give method to verify that getApples gets the correct result.

    Here’s a gag: “The problem with object-oriented programming languages is that they always come with all the hidden environments that they need. You want a banana and you get a gorilla with a banana and a whole jungle.” “– Joe Armstrong, Inventor of Erlang

Let’s talk about the disadvantages of functional thinking code

  • I just give you add(n, 1), and if I don’t know what you’re doing, I don’t know what you’re doing.
  • If you don’t write down your thoughts, you have no idea how this topic relates to add and sub functions

From the pros and cons of the above analysis, we can begin to understand why functional programming can be effective. But be aware that functional programming requires more comments than object-oriented programming, and clearly describe how a practical problem is abstracted into a mathematical one, and what the derivation is. The comparison also shows that object-oriented code is relatively easy to understand, and being easy to understand is a very important advantage, because we often say: write code that people can understand.

If one of the conditions of the application problem is changed to “passing by Zhang Brick’s house, Xiaoyu gave Zhang Brick 5 apples”, the cost of changing the two ways of thinking is also different. For object oriented thinking, you need to find the code that describes this action and modify it; Functional thinking may require rederiving the flow.

Object-oriented thinking is trying to tell us: Tell us your world, we’ll describe it in code functional thinking is cold telling us: Tell us your world, we’ll turn it into math

This example should cause a lot of confusion, this is a word problem, simple use of addition, subtraction, multiplication and division, has nothing to do with functional programming. In fact, I am also very distressed, how to round this thing back ah

If you agree that this problem with addition and subtraction to do is the most normal thinking, congratulations you have a functional thinking, we have naturally received the topic xiaoming apple abstract into addition, Xiaoming to go out apple abstract into subtraction. You’ve already done the first step — abstracting a real problem into a mathematical one. This is a country that pays $98 to the cashier, $103 to the cashier, and $5 to change every second. The four operations, though not quite as fancy, have something to do with functional programming. The operation symbol + and the operation symbol – are treated as functions. The numbers on either side of the operation symbol are the input and the result is the output. If you’re interested, take a look at Haskell’s use of the four operators, which will make it a little more intuitive.

5. Finally, are Map and Reduce functional programming?

This is a good question and an interesting one, and there are indeed many functional programming articles that explain and emphasize the use of these functions. I want to explain this with another question: Are classes object-oriented? A lot of people will tell me that classes are an important concept in object-oriented programming, but you shouldn’t just use classes to distinguish object-oriented thinking, that is, if you use classes, if you don’t understand object-oriented thinking, you will still write a lot of non-object-oriented code. I think we can explain map and Reduce as well: these are some of the more common functions in functional programming, but if you don’t understand functional programming, you can still write a lot of non-functional code with Map and Reduce.

6. Recommend

There’s a book on Haskell, a functional language that I think you can read. A quick look at the design of the Haskell language will give you some insight. Seeing Haskell functions as inherently Currified may make you wonder about the language, but think about JS currified and see that (+3) can be used as a function may make you wonder about the world

See how celebrities make fun of object orientation

Dumb functional programming

JS functional programming guide

HASKELL’s Guide to Fun Learning

Functor, Applicative, and Monad’s interpretation of pictures


Finally, in order to facilitate recruitment, the team set up a public account, mainly some recruitment information, team information, all technical articles can be seen in the public account, by the way, if you want to go to meituan other teams, we can also help you push oh ~

Qr code