On functional programming

Source: Internet
Author: User

on "Functional Programming "

1. function is "first Class citizen"

The so-called "first class citizen" refers to a function that, like other data types, is equal, can be assigned to other variables, can also be passed as a parameter to another function, or as a return value for another function.

2. Use "expression" only, without "statement"

Expression is a simple operation and always has a return value;"statement" (statement) is an operation that does not return a value . Functional programming requires that only expressions be used and no statements are used. In other words, each step is a simple operation, and there are return values.

The reason is that the development motive of functional programming is to deal with the operation (computation), regardless of the system's read-write (I/O). "Statement" is a read and write operation to the system, so it is excluded.

Of course, in practical applications, it is not possible to do I/O. Therefore, in the programming process, functional programming only requires to limit the I/O to the minimum, do not have unnecessary read and write behavior, maintain the simplicity of the computational process.

3. No "Side effects"

The so-called "side effect" (side effect), refers to the function inside and outside interaction (the most typical case is to modify the value of the global variable), resulting in other results than the operation.

Functional programming emphasizes that there is no "side effect", meaning that the function is to remain independent, all functions are to return a new value, no other behavior, especially the value of the external variable must not be modified.

5. Referencing transparency

Reference transparency (referential transparency), refers to the function's run does not depend on the external variable or "state", only depends on the input parameters, any time as long as the parameters are the same, the reference function will always get the same return value.

"The meaning of functional programming "

1. Simple code, rapid development

Functional programming uses a large number of functions, reducing the duplication of code, so the program is relatively short and development speed is faster. For example, the sort method, you only need to write once.

2. Close to natural language, easy to understand

Functional programming has a high degree of freedom to write code that is close to natural language.

  

3. More convenient code management

Functional programming is not dependent, and does not change the state of the outside world, as long as the input parameters are given, the returned result must be the same. Therefore, each function can be considered as a standalone unit, which is advantageous for unit tests (unit testing) and debugging (debugging), as well as modular combinations.

4. Easy "Concurrent Programming"

Functional programming does not need to consider "deadlock" (deadlock), because it does not modify variables, so there is no "lock" thread problem. Instead of worrying about a thread's data being modified by another thread, you can confidently spread the work across multiple threads and deploy concurrent programming (concurrency).

Reference: http://www.ruanyifeng.com/blog/2012/04/functional_programming.html

On functional programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.