这些概念,有些了解,有些不了解,老是晕乎乎的。接下来咱们就去寻找答案。 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> {
}
复制代码