Responsive programming

Responsive programming is a programming paradigm for data flow and change propagation. This means that static or dynamic data flows can be easily expressed in a programming language, and the associated computational model automatically propagates the changing values through the data flows.

For example, the expression a=b+c is processed. In imperative programming, the result of B + C is calculated and then assigned to variable A. Therefore, changes in the values of B and C will not affect variable A. But in reactive programming, the value of variable A changes with changes in b and C.

A spreadsheet program is an example of reactive programming. Cells can contain literals or formulas like “=B1+C1”, and the value of the cell containing the formula changes depending on the value of the other cells.

Reactive programming was originally proposed as a way to simplify the creation of interactive user interfaces and the drawing of real-time system animations, but it is essentially a universal programming paradigm.

For example, in the MVC software architecture, responsive programming allows changes to related models to be automatically reflected into the view and vice versa.

Functional programming

Functional programming is a form of programming that treats computer operations as if they were functions. The most important foundation of functional programming languages is lambda calculus, and functions of the lambda calculus can accept functions as inputs (parameters) and outputs (return values).

Compared with instruction programming, functional programming emphasizes the importance of function calculation over instruction execution.

In contrast to procedural programming, functional programming allows a function to be evaluated at any time.

Closures and higher-order functions

Functional programming supports functions as first class objects, sometimes called closures or functor objects. Essentially, closures are objects that act as functions and can be manipulated like objects. Similarly, the FP language supports higher-order functions. A higher-order function may take another function (indirectly, an expression) as its input argument, and in some cases it may even return a function as its output argument. The combination of these two constructs makes modular programming possible in an elegant manner, which is the biggest benefit of using FP

The inertia calculation

In addition to the concepts of higher-order functions and functors (or closures), FP also introduces the concept of lazy computing. In lazy computation, the expression is not evaluated immediately when it is bound to a variable, but when the evaluator needs to produce the value of the expression. Deferred computation allows you to write functions that could potentially produce an infinite output. Because you don’t compute more than the rest of the program needs, you don’t need to worry about out-of-memory errors caused by infinite computations. An example of lazy computing is a function that generates an infinite Fibonacci list, but the calculation of the NTH Fibonacci number is equivalent to just extracting an item from a possible infinite list.

recursive

FP is also characterized by the use of recursion as a mechanism for controlling the flow. For example, Lisp deals with lists defined as having sublists after the header element, a notation that naturally recurses itself over smaller sublists.

Functional programming has five distinct characteristics.

The function is first class citizen

A “first class” refers to the fact that functions are equal to other data types and can be assigned to other variables, passed as arguments to another function, or returned as a value from another function.

Just use “expression”, not “statement”

“Expression” is a pure operation that always returns a value; A statement is an operation that does not return a value. Functional programming requires expressions, not statements. In other words, each step is pure operation and has a return value.

And the reason is because functional programming was motivated to try to compute, not to think about I/O.” The statement “reads and writes to the system and is excluded.

Of course, in practice, it is impossible not to do I/O. Therefore, in the programming process, functional programming only requires to limit I/O to a minimum, do not have unnecessary read and write behavior, and maintain the simplicity of the calculation process.

There are no side effects.

A “side effect” is an interaction between the inside and outside of a function (most typically, modifying the value of a global variable) that produces a result other than the operation.

Functional programming emphasizes the absence of “side effects,” meaning that functions remain independent, all they do is return a new value, and nothing else, especially not changing the value of an external variable.

Do not modify the state

As mentioned above, functional programming simply returns new values and does not modify system variables. Therefore, it does not modify variables, is also an important feature of it.

In other types of languages, variables are often used to hold “states.” Leaving variables unchanged means that state cannot be stored in variables. The best example of functional programming that uses parameters to preserve state is recursion. The following code is a function that inverts strings, demonstrating how different parameters determine the “state” of an operation.

Referential transparency

Function programs also typically enforce reference transparency, meaning that if the same input is provided, the function always returns the same result. That is, the value of an expression does not depend on the global state that can change the value. This allows you to formally infer program behavior, because the meaning of an expression depends only on its children and not on the order of evaluation or the side effects of other expressions. This helps verify correctness, simplify the algorithm, and even help figure out how to optimize it.

Side effects

The side effect is to modify the language structure of the system state. Because the FP language does not contain any assignment statements, the value of a variable is never changed once it is assigned. Moreover, calling the function only computes the result — nothing else. Therefore, FP languages have no side effects

advantages

  1. Simple code, fast development
  2. Close to natural language and easy to understand
  3. Easier code management
  4. Easy “concurrent programming”
  5. Hot upgrade of code
    • Functional programming has no side effects, as long as the interface stays the same and the internal implementation is externally independent. As a result, code can be upgraded directly while running, with no need for reboot or downtime. Erlang, developed by Swedish company Ericsson to manage the telephone system, has long proved this. Upgrades to the phone system, of course, cannot be stopped

concerns

  • Early functional programming languages were not implemented with efficiency in mind.
  • For speed, some non-functional programming languages do not provide automatic boundary checking or automatic garbage collection.