Talk about responsive and functional

Analysis & Answers


Reactive Programming (RP for short)

In computers, reactive programming is a programming paradigm oriented toward the flow of data and the propagation of changes. This means that static or dynamic data flows can be easily expressed in programming languages, and the associated computing model will automatically propagate changing values ​​through the data flow. Reactive programming is a programming thinking mode. Different from declarative programming, reactive programming pays more attention to data flow. Each program is designed around data. Where is the original data? After what processing, what is the final result? What it became. A program that revolves around data flow like this can be called an execution sequence for short.

From a design perspective, the data return value of reactive programming actually returns a data publisher ( Publisher ), and the caller is equivalent to a data subscriber ( Subscriber ) (there are also hot and cold sequences and push-pull modes here) (difference), only when the subscriber subscribes to the data ( Flux#subscribe() ), the data provided by the publisher and the corresponding execution sequence will actually be executed. There is another name called Lazy Compute , delayed calculation.

The traditional programming method is executed sequentially. If the previous task is not completed, you need to wait until it is completed before executing the next task. Whether it is improving the performance of the machine or the performance of the code, it essentially depends on the completion of the previous task. If you need to respond quickly, you have to change the synchronous execution method to asynchronous, and method execution becomes message sending. This becomes asynchronous programming, which is one of the important features of reactive programming.

Reactive programming has the following characteristics:

  • Asynchronous programming: Provides a suitable asynchronous programming model that can tap the capabilities of multi-core CPUs, improve efficiency, reduce delays and blocking, etc.
  • Data flow: Based on the data flow model, reactive programming provides a unified set of Stream-style data processing interfaces. Compared with Stream in Java 8, reactive programming not only supports static data flow, but also supports dynamic data flow, and allows reuse and simultaneous access to multiple subscribers.
  • Change propagation: Simply put, it is the process of taking a data stream as input, converting it into another data stream through a series of operations, and then distributing it to each subscriber. This is a bit like the combined function in functional programming, which connects multiple functions to transform a set of input data into output data in different formats.

Functional Programming (FP for short)

As hardware capabilities continue to improve, the computing power of single-core CPUs has almost reached its limit. CPUs have entered the multi-core era, and programmers have turned to concurrent programming and distributed systems to deal with increasingly complex computing tasks.

However, concurrent programming is not a silver bullet. As a kind of concurrent programming based on shared memory, multi-thread programming has common problems such as deadlock, thread starvation, race conditions, etc., and multi-thread bugs are difficult to reproduce and locate.

Thus, functional programming began to rise. In functional programming, since all data is immutable, there are no problems with concurrent programming and it is thread-safe. It regards computer operations as the calculation of functions in mathematics. The main feature is that the calculation process is decomposed into multiple reusable functions, and the concept of state and variables is avoided. Although functional programming can also be attributed to process-oriented programming, its ideas are closer to mathematical calculations.

Features of functional programming:

  • Functions are "first class citizens": The so-called "first class" refers to the fact that functions are on an equal footing with other data types and can be assigned to other variables or passed as parameters to another function, or as the return value of another function.
  • Closures and higher-order functions: Closures are objects that behave like functions and can be manipulated like objects. Similarly, FP languages ​​support higher-order functions. A higher-order function can take another function (indirectly, with an expression) as its input parameter, and in some cases it even returns a function as its output parameter. The combination of these two structures enables modular programming in an elegant way, which is the biggest benefit of using FP.
  • Recursion: Use recursion as a mechanism for controlling flow. For example, in the world of Haskell, there are no variable assignments or process jumps. If you want to implement some simple functions, such as finding the maximum value in an array, you need to use recursion.
  • Lazy Evaluation: It is represented by "lazy evaluation" and "minimization evaluation". Lazy evaluation gives code great optimization potential. Compilers that support lazy evaluation view functional programming programs the same way mathematicians view algebraic expressions: canceling out identical terms to avoid unnecessary code execution, and arranging code execution order to achieve higher execution efficiency or even reduce errors. Another important benefit of lazy evaluation is that it can construct an infinite data type without worrying about out-of-memory errors caused by infinite calculations.
  • No "side effect" (side effect): refers to the interaction between the inside and outside of the function (the most typical case is to modify the value of the global variable), producing other results other than operations. Functional programming emphasizes that there are no "side effects", which means that the function must remain independent. All functions are to return a new value and have no other actions, especially the values ​​of external variables must not be modified.

Functional Reactive Programming (FRP for short)

Functional reactivity combines the advantages of functional and reactive programming. Combining a set of ideas in the functional paradigm with reactive programming is functional reactive programming.

We know that traditional object-oriented programming solves problems by abstracting object relationships. Functional programming solves problems through the combination of functions, and reactive programming solves the problem of callback hell through functional programming.

It is not very intuitive to use traditional object-oriented methods to handle asynchronous events, and dealing with concurrency is also troublesome, so functional reactive programming was born.

Reflect & Expand


summary

Functional programming and reactive programming are two different concepts. When I first came into contact with RxJava, I often confused the two concepts.

The combination of the two and functional reactive programming indeed brings a new way and a breakthrough in thinking to development.


Meow Interview Assistant: One-stop solution to interview questions. You can search the WeChat applet [Meow Interview Assistant] or follow [Meow Interview Assistant] -> Interview Assistant to answer questions for free. If you have any good interview knowledge or skills, I look forward to sharing them with you!

Guess you like

Origin blog.csdn.net/jjclove/article/details/124388096#comments_28149403