這些概念,有些瞭解,有些不瞭解,老是暈乎乎的。接下來咱們就去尋找答案。 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
函數式編程關心數據的映射,命令式編程關心解決問題的步驟markdown
![]() Alonzo Church(1903-1995) |
![]() Haskell Brooks Curry(1900-1982) |
---|
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的設計理念:
動態和變化的值是"一等公民".你能夠定義和組合他們以及做爲函數的輸入輸出。我稱之「行爲」。
行爲由一些原語構成,好比 常量(靜態)行爲和時間(好比時鐘)而後還有將其串行和並行的組合。
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.
構成要素要通用且簡介。
慕課網有個帖子,對FRP有段說明:
FPR 將輸入分爲兩個基礎的部分:行爲(behavior)和事件(events) 。這兩個基本元素在函數響應式編程中都是第一類(first-class)值。 其中行爲是隨時間連續變化的數據,而事件則是基於離散的時間序列 。例如:在咱們操做網頁的時候,會觸發不少的事件,包括點擊,拖動,按鍵事件等。這些事件都是不連續的。對事件求值是沒有意義的,全部咱們通常要經過fromEvent,buffer等將其變成連續的行爲來作進一步處理。
Conal Elliott (82年本科畢業於 UCSB College of Creative Studies 加州大學聖塔芭芭拉分校創意設計學院 )
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等
如今咱們不得不提 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…
Erik Meijer,(1963-至今,生於荷蘭庫拉索島)
RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
這裏咱們須要提一下,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> {
}
複製代碼