The introduction

What do we talk about when we talk about RxJava?

  • Functional programming, responsive programming
  • Callback Hell
  • Works with Retorfit for Android web requests
  • Thread switching, click anti-shake and so on

These concepts, some understand, some do not understand, always confused. Now we’re going to find out.

Let’s start with the math

We have a mathematical function f(x)=(x+2)*3-4, code to implement it.

// The former: traditional procedural programming might be written like this
var x = x + 2
	x = x * 3
    x = x - 4
return x

// The latter: the so-called functional programming may be written like this
return add(x,2).multiply(3).subtract(4)

/ / RxJava how to write
 Observable
 	.just(x)
 	.map { it + 2 }
  	.map { it * 3 }
  	.map { it - 4 }
  	.subscribe { println(it) }
Copy the code

Looking at an example, we have a set of numbers (1,2,3,4,5) counting the number of numbers greater than 3

var values = arrayOf(1.2.3.4.5)

// The former: imperative/procedural programming
    var count = 0;
    for (value in values) {
        if (value > 0) {
            count++
        }
    }
    return count

// The latter: chained programming?
return values.filter { it > 3 }.count()

Copy the code

� We seem to feel something different

  • The former: it seems that the logical translation is carried out step by step
  • The latter: Fill your head with add,map,fliter,count functions and get results.

All men are mortal

Functional programming

Functional programming

Functional programming is concerned with mapping data, imperative programming is concerned with problem-solving steps

  • The theoretical foundation of combinatorics from 1920s to 1930s (Moses Schonfinkel, Haskell Curry)
  • Lambda calculus, theoretical foundation of the 1930s (Alonzo Church)
  • The early functional language LISP was developed by John McCarthy at MIT in the late 1950s
  • From 1950s to 1980s, various functional languages IPL, APL, FP, ML, SASL, Miranda…
  • 1990 Functional language Haskell (standardized on programming language Miranda)


Alonzo Church(1903-1995)


Haskell Brooks Curry(1900-1982)





Responsive programming

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

  • The reactive programming paradigm is based on the Synchronous Data Flow proposed by Edward A. Lee and David G. Messerschmitt in 1987, but with A relaxed real-time constraint. The reactive programming paradigm adds behavior representing continuous time-varying values and events representing discrete values.

For example, in a model — view — Controller (MVC) architecture, reactive programming can facilitate changes in an underlying _model _that are reflected automatically in an associated view

Functional responsive programming

Functional Reactive Programming(FRP) is a Programming paradigm that uses basic Functional Programming components (such as Map, Reduce, and filter) to perform Reactive Programming(asynchronous data flow Programming)

  • It was first proposed in 1997 by Conal Elliott and Paul Hudak in ICFP 97 paper Functional Reactive Animation.

Conal Elliott graduated from UCSB College of Creative Studies(UCSB) in 1982 and PhD in 1990. It’s also very active, with Github submitting more than 600 entries in 2021 (as of April).

In 2009, someone asked stackOver Follow What is Functional Programming? This guy responded with a wave. He listed some FRP design concepts from the design perspective:

  • Dynamic/evolving values (i.e., values “over time”) are first class values in themselves. You can define them and combine them, pass them into & out of functions. I called these things “behaviors”.

Dynamic and variable values are “first-class citizens “. You can define and combine them as well as function inputs and outputs. I call it “behavior.”

  • Behaviors are built up out of a few primitives, like constant (static) behaviors and time (like a clock), and then with sequential and parallel combination. _n_behaviors are combined by applying an n-ary function (on static values), “point-wise”, i.e., continuously over time.

Behavior consists of primitives such as constant (static) behavior and time (such as clock) and then a combination of serial and parallel.

  • To account for discrete phenomena, have another type (family) of “events”, each of which has a stream (finite or infinite) of occurrences. Each occurrence has an associated time and value.

  • To come up with the compositional vocabulary out of which all behaviors and events can be built, play with some examples. Keep deconstructing into pieces that are more general/simple.

The components should be generic and brief.

  • So that you know you’re on solid ground, give the whole model a compositional foundation, using the technique of denotational semantics, which just means that (a) each type has a corresponding simple & precise mathematical type of “meanings”, and (b) each primitive and operator has a simple & precise meaning as a function of the meanings of the constituents.

There is a post on THE MOOC website that explains FRP:

FPR divides the input into two basic parts: behaviors and events. Both of these basic elements are first-class values in functional responsive programming. Behavior is data that changes continuously over time, while events are based on discrete time series. For example: when we operate the web page, will trigger a lot of events, including click, drag, button events and so on. These events are discontinuous. It doesn’t make sense to evaluate events, so we usually do further processing by fromEvent, buffer, etc.



Conal Elliott(Graduated from UCSB College of Creative Studies in 1982, College of Creative Design, University of California, Santa Barbara)

Reactive Extensions

Rx = Observables + Operators + Schedulers. ReactiveX is a combination of the best ideas from the Observer pattern, The Iterator Pattern, and Functional Programming ReactiveX provides an asynchronous programming API for observing flow. It is inspired by the ideas of observer pattern, Iterator pattern, and functional programming. As a general purpose library, ReactiveX is now available in several languages (all open source), including RxJava, RxCpp, RxSwift, RxKotlin, RxGroovy, RxJavaScript, and so on

  • Around 2010, Volta was developed by Microsoft Live Labs, which later disbanded and the Volta project died.
  • Around 2011, Microsoft Cloud Programmability Team invented Reactive Extensions from Volta. On June 21, 2011, Microsoft Cloud Programmability Team launched Reactive Extensions. The.net Framework of Rx.NET.
  • In 2012, Microsoft Cloud Programmability Team opened source the implementation of Rx’s other platforms (including JavaScript and C++)

Now we have to mention Erik Meijer (born 18 April 1963, Curacao, Netherlands) is a Dutch computer scientist and entrepreneur. From 2000 to early 2013 worked at Microsoft C#, Visual Basic, LINQ, Volta, and led Microsoft Cloud Programmability Team. You’d be right to call him the father of Rx. In early 2013, Erik Meijer left Microsoft to start Applied Duality Incorporated. During that time, he worked with Facebook on the Hack language, Netflix on the RxJava library, and Google on the Dart language.

[

] (En.wikipedia.org/wiki/Erik_M…



Erik Meijer(1963-present, Born in Curacao, Netherlands)



RxJava

RxJava — Reactive Extensions for the JVM — a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

  • In 2012 Netflix began to respond to growing demand for its services. NET Rx is migrated onto the JVM.
  • RxJava was first introduced to the world in February 2013 by Ben Christensen and Jafar Husain in a post on Netflix’s technology blog.
  • In late 2013, engineers from Netflix, Pivotal, Typesafe, and others started a reaction-Streams specification initiative and discussion, and the Reaction-Streams JVM project was created on Github. By May 2015, version 1.0 of the specification was available. RxJava of Netflix, Akka Stream of Typesafe, inventor of Scala programming language, Project Reactor of Pivotal, vert. x and other responsive development libraries all follow the sub-specification.
  • November 12, 2016, the 2.x release of RxJava,
    • Reaction-streams specification has been extensively rewritten,
    • Flowable supports backpressure, which is no longer supported by Observables.
    • Added Operator optimization operator-fusion.
  • In June 2019, RxJavA3.x was released

Here we should mention some thoughts on reactive programming and operator fusion by David Karnok, a major contributor to RxJava 2.x

reactive-streams-jvm

public interface Publisher<T> {
     void subscribe(Subscriber<? super T> s);
}
public interface Subscriber<T> {
    void onSubscribe(Subscription s);
     void onNext(T t);
     void onError(Throwable t);
     void onComplete();
}
public interface Subscription {
     void request(long n);
     void cancel();
}
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {}Copy the code

From functional to RxJava