Functional paradigm oriented programming (functional programming)

Source: Internet
Author: User

Function programming (FP) not only refers to the language such as Haskell Scala, but also represents a kind of programming thinking, software thinking method, also called function-oriented programming. The essence of programming is combination, the essence of composition is category category, and category is the combination of function.

First, what is functional programming, which does not have a unique definition, is just a broad aggregation of some of the programming style features that we can compare with OOP, the difference being that OOP focuses primarily on the differences between data, while the FP focuses on the consistency of the structure.

Object-oriented:

    1. Tightly coupled data and operation of the data
    2. . Objects hide implementation Details of their operations, and other objects invoke these operations only through interfaces.
    3. The core abstract model is the data itself
    4. Core activities are the combination of new objects and the expansion of existing objects, which are implemented by adding new methods.

Function Programming:

    1. The data is loosely coupled to the function
    2. Functions hide their implementations, language abstractions are functions, and functions are combined to express them.
    3. The core abstract model is a function, not a data structure
    4. The core activity is to write new functions.
    5. Variable default is constant, reducing the use of variable variables, concurrency is good

So are OOP and FP winners in the business world? Most of our business logic is written like this:


SELECT orders.order_id, Orders.order_date, Suppliers.supplier_name
From suppliers
Right OUTER JOIN Orders
On suppliers.supplier_id = orders.supplier_id
WHERE orders.order_status = ' incomplete '
ORDER by Orders.order_date DESC;

SQL is very similar to FP, it can penetrate into the business, it uses a consistent data structure (table structure schema), some basic functions can be combined into many query statements, it is declarative declarative, that is, write the SQL is to tell the database what I need, the database for you to return Without having to specify how the database is specifically queried.

What are the differences between declarative programming and imperative programming? The main feature of FP is that they describe what they want, not how they are implemented. OO, in its approach, uses most of the imperative techniques. Here is the command-line technical code:

var sumofsquares = function (list) {
var result = 0;
for (var i = 0; i < list.length; i++) {
Result + = Square (list[i]);
}
return result;
};

Console.log (Sumofsquares ([2, 3, 5]));

The function programming code is as follows:
var sumofsquares = pipe (Map (square), reduce (add, 0));
Console.log (Sumofsquares ([2, 3, 5]));

Function-style programming features:

    1. First class citizens are functions
    2. lambdas/anonymous function with closures
    3. Invariance, mostly stateless, with no states and variables
    4. High concurrency
    5. No side-effect calls
    6. Recursive performance optimizations are achieved through tail call.
    7. Pattern matching (Haskell, Erlang)
    8. Lazy Assignment (Miranda, Haskell)
    9. Homoiconicity (like Lisp)

If there are many people in OOP may be affected by the static data thinking, then the FP brings a completely dynamic event, FP let us directly with the verb thinking, using method functions to solve the problem, such as the transfer between two accounts, according to the static domain such as DDD modeling thinking, transfer This function is placed in the account of this entity class, Or is it a service? In the OOP language, we implement functionality that is always replaced by a concept such as service services, and with the emphasis on stateless services, the stateless service is actually an empty shelf "class" that only the method function has no attributes. Adam Heroku in 2007 wrote: The old-fashioned way of transferring money between bank accounts is to use database transactions, which is more rigid, and the right way to do this is to store the transfer events if you're a functional paradigm thinker who thinks it's normal. ---from the event-based transaction implementation of NoSQL storage.

A lot of people attributed FP to mathematical thinking, actually this only saw its surface, did not see the mathematical language behind the formal logic, programming language as the same as the mathematical form of language, their core basis are analytic philosophy of formal logic, the past object-oriented many design principles are also derived from formal logic, see: Quine and citation Transparent.

Object-oriented and function-oriented have been arguing, in fact purely oop and purely FP are extreme, for OOP: the existence and must be the object, the function is not the object; for FP: the existence is not always pure, the side effects are always real. In short, object oriented focuses on decomposition, and functional programming focuses on composition.

Functional paradigm oriented programming (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.