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:
- Tightly coupled data and operation of the data
- . Objects hide implementation Details of their operations, and other objects invoke these operations only through interfaces.
- The core abstract model is the data itself
- Core activities are the combination of new objects and the expansion of existing objects, which are implemented by adding new methods.
Function Programming:
- The data is loosely coupled to the function
- Functions hide their implementations, language abstractions are functions, and functions are combined to express them.
- The core abstract model is a function, not a data structure
- The core activity is to write new functions.
- 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:
- First class citizens are functions
- lambdas/anonymous function with closures
- Invariance, mostly stateless, with no states and variables
- High concurrency
- No side-effect calls
- Recursive performance optimizations are achieved through tail call.
- Pattern matching (Haskell, Erlang)
- Lazy Assignment (Miranda, Haskell)
- 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)