RxJava介紹1:前世此生

引言

當咱們談RxJava時,咱們談些什麼?

  • 函數式編程、響應式編程
  • Callback Hell (回調地獄)
  • 配合Retorfit,用於Android網絡請求
  • 線程切換、點擊防抖等等

這些概念,有些瞭解,有些不瞭解,老是暈乎乎的。接下來咱們就去尋找答案。 html

從數學運算講起

咱們有一個數學函數 f(x)=(x+2)*3-4,代碼來實現它。react

//前者:傳統的過程式編程,可能這樣寫
var x = x + 2
	x = x * 3
    x = x - 4
return x

//後者:所謂函數式編程,可能這樣寫
return add(x,2).multiply(3).subtract(4)

//RxJava 怎麼寫
 Observable
 	.just(x)
 	.map { it + 2 }
  	.map { it * 3 }
  	.map { it - 4 }
  	.subscribe { println(it) }
複製代碼

在看一個例子,咱們有一組數字(1,2,3,4,5) 統計大於3的數字個數git

var values = arrayOf(1, 2, 3, 4, 5)

//前者:命令式/過程式編程
    var count = 0;
    for (value in values) {
        if (value > 0) {
            count++
        }
    }
    return count

//後者:鏈式編程?
return values.filter { it > 3 }.count()

複製代碼

�咱們彷佛感覺到一些不一樣github

  • 前者:彷佛在一步步把邏輯翻譯計算執行過程
  • 後者:腦子裏面充斥着add,map,fliter,count這些函數,最後獲得結果。

前世此生

函數式編程

Functional programming編程

函數式編程關心數據的映射,命令式編程關心解決問題的步驟markdown

image.png
Alonzo Church(1903-1995)
image.png
Haskell Brooks Curry(1900-1982)

截屏2021-05-08 下午4.48.19.png

響應式編程

Reactive Programming
響應式編程(reactive programming)是一種基於數據流(data stream)和變化傳遞(propagation of change)的聲明式(declarative)的編程範式
網絡


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 app

函數式響應式編程

Functional Reactive Programming(FRP)
函數式響應式編程(FRP) 是一種採用函數式編程的基礎部件(如map、reduce、filter等)進行響應式編程(異步數據流程編程)的編程範式異步

Conal Elliott 82年本科畢業於 UCSB College of Creative Studies(加州大學聖塔芭芭拉分校創意設計學院),90年博士畢業。目前也很是活躍,Github 2021年已經提交了600屢次(截止4月份)。jvm

2009年左右有人在stackover follow上問What is functional reactive programming? 這哥們順勢回答了一波。
他從設計的角度列舉了一些FRP的設計理念:

  • 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".

動態和變化的值是"一等公民".你能夠定義和組合他們以及做爲函數的輸入輸出。我稱之「行爲」。

  • 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.

行爲由一些原語構成,好比 常量(靜態)行爲和時間(好比時鐘)而後還有將其串行和並行的組合。

  • 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.

構成要素要通用且簡介。

  • 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.


慕課網有個帖子,對FRP有段說明:

FPR 將輸入分爲兩個基礎的部分:行爲(behavior)和事件(events) 。這兩個基本元素在函數響應式編程中都是第一類(first-class)值。 其中行爲是隨時間連續變化的數據,而事件則是基於離散的時間序列 。例如:在咱們操做網頁的時候,會觸發不少的事件,包括點擊,拖動,按鍵事件等。這些事件都是不連續的。對事件求值是沒有意義的,全部咱們通常要經過fromEvent,buffer等將其變成連續的行爲來作進一步處理。

image.png
Conal Elliott (82年本科畢業於 UCSB College of Creative Studies 加州大學聖塔芭芭拉分校創意設計學院 )

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提供可觀測流的異步編程API,它的靈感來源於觀察者模式、迭代器模式和函數編程這些優秀思想。ReactiveX做爲一個通用庫, 如今已經有多種語言的實現版本(都是開源的), 包含RxJava, RxCpp, RxSwift, RxKotlin, RxGroovy, RxJavaScript等

  • 2010年左右,Microsoft Live Labs開發了Volta項目,後來labs解散了,volta項目也黃了。
  • 2011年左右,Microsoft Cloud Programmability Team 借鑑或者說傳承Volta中關於響應式擴展的部分,發明了Reactive Extensions,2011年6月21號,發佈了最初針對.NET Framework的Rx.NET.
  • 2012年,Microsoft Cloud Programmability Team又開源了Rx其餘平臺的實現(包括JavaScript和C++)

如今咱們不得不提 Erik Meijer,(生於1963年4月18日,荷蘭庫拉索島)是荷蘭計算機科學家和企業家。 From 2000 to early 2013 在微軟工做,搞過C#,Visual Basic,LINQ,Volta,領導過Microsoft Cloud Programmability Team。你要是說他是Rx之父,也不爲過。 2013年初,Erik Meijer離開了微軟,創立了Applied Duality Incorporated。在此期間,他與Facebook合做開發了Hack語言,與Netflix合做開發了RxJava庫,並與Google合做開發了Dart語言。

[

](en.wikipedia.org/wiki/Erik_M…
image.png
Erik Meijer,(1963-至今,生於荷蘭庫拉索島)

截屏2021-05-08 下午5.03.09.png

RxJava

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

  • 2012年Netflix爲了應對不斷增加的業務需求開始將.NET Rx遷移到JVM上面。
  • 2013年二月份,Ben Christensen 和 Jafar Husain發在Netflix技術博客的一篇文章第一次向世界展現了RxJava。
  • 2013年底的時候,Netflix、Pivotal、Typesafe等公司的工程師們共同發起了制定「Reactive-Streams規範」的倡議和討論,並在github上建立了reactive-streams-jvm項目。到2015年5月份,1.0版本的規範出爐。Netflix的RxJava、Scala編程語言的發明者Typesafe公司的Akka Stream、Java開發者都熟悉的Pivotal公司的Project Reactor、走在技術前沿的Vert.x等響應式開發庫都遵循次規範。
  • 2016年11月12日,RxJava的2.x發佈,
    • 按照Reactive-Streams規範進行了大量的重寫,
    • 新增Flowable支持backpressure,Observable再也不支持此特性。
    • 新增運算符優化Operator-Fusion.
  • 2019年6月,RxJava3.x發佈

這裏咱們須要提一下,RxJava 2.x的主要貢獻者David Karnok 關於響應式編程以及運算符融合的一些思考

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> { 
}
複製代碼

截屏2021-05-08 下午5.02.19.png

從函數式到RxJava


截屏2021-05-08 下午6.04.45.png

相關文章
相關標籤/搜索