preface

When I started using RAC a year ago, I knew that ReactiveCocoa was a framework inspired by functional responsive programming, but I didn’t know much about it. Recently, I began to learn and use RXSwift, starting from the most basic functional responsive programming ideas.

Ideas about Functional Programming

  • Functional programming, or functional programming, is a programming paradigm that treats computer operations as functional operations and avoids the use of program states and mutable objects.

  • We know that y = f(x), and x = f(x), so y = f(f(x)).

    It is possible to pass a function as an argument to another function and to return a function.

  • No side effects

func factorial(a:Int)->(Int){
           if a == 1{
               return1}return 1 * sum(a:a - 1)
       }
Copy the code

The above code is a factorial recursive function, which shows that in functional programming there is basically no state quantity, no assignment statement, only expression. It also reflects that functional programming uses recursion as a mechanism for flow.

  • Closures and higher-order functions

    • Closures are objects that act as functions and can operate like objects.
    • A higher-order function may take another function (indirectly, an expression) as its input parameter, and in most cases it may even return a function as its output parameter.
  • Reference transparent

    • If a function is only changed by its input arguments, then the function is called the same every time.
    • A function f(x) calls g(x),g(x) calls H (x), and h(x) finally computes the result and returns it as f(x).
    • If all the states are unchanged, the next time f(x) calls the same argument, it should get exactly the same result, so it doesn’t need to call g(x) and H (x) anymore, but it can get exactly the same result. In general, if the input parameter is the same, the return value must be the same. If the function has this property, it can be said to be reference-transparent.

Ideas about Reactive Programming

  • Reactive Programming is about writing Programming patterns that respond to events such as user input, data flow, changes in system state, and so on.

  • The first core feature of responsive programming is change passing. The simple understanding is that after one event in a series of events changes, a series of events will change, kind of like dominoes in real life.

  • The second core feature of responsiveness is data flow. You can create a data flow based on anything. Streams are lightweight and ubiquitous, and anything can be a stream: variables, user input, properties, caches, data structures, and so on

  • The third core feature of responsiveness is the declarative programming paradigm. The computational logic is constant no matter what element is passed, creating a kind of binding to the computational logic that feels immutable.

Reactive programming is a programming paradigm declarative based on data stream and propagation of change.

The “change passing” of responsive programming is the plumbing of a juice line; Put oranges in your mouth and orange juice comes out. When you put in the watermelon, what comes out is watermelon juice, oranges and watermelons, as well as pulp, juice and residue in the machine, all of which are flowing “data streams”. The drawing of the pipe is expressed in declarative language. In our daily development, if we can build a channel from the data layer to the view layer, then we do not need to refresh the UI through the command to change the data, we can change the data, use this channel to automatically transfer the change to the view, and then automatically refresh the interface. Of course, you can establish a two-way channel, View changes can also achieve automatic data modification.

Functional Reactive Programming

Frp-functional Reactive Programming is an example of frP-functional Reactive Programming. Reactive programming is mainly realized by using functions of functional programming as basic modules. The purpose is to simplify the programming steps, such as the real-time data update process during interface programming. In simple terms, the use of functions to achieve automatic asynchronous data updates.

conclusion

In fact, the main purpose of this article is to study RxSwift before the RX family functional response programming ideas have a certain understanding, in the use and in-depth mining of RxSwift can play a role.

Recommend an English about reactive programming thought piece: gist.github.com/staltz/868e…