ReactiveX 学习笔记(6)条件操作符
Conditional and Boolean Operators
本文的主题为处理 Observable 的条件和布尔操作符。
这里的 Observable 实质上是可观察的数据流。
RxJava操作符(七)Conditional and Boolean
公共代码
fun <T> Observable<T>.dump() =
this.subscribe({ println("onNext: $it") },
{ println("onError: $it: ${it.message}") },
{ println("onComplete") })
fun <T> Single<T>.dump() =
this.subscribe({ println("onNext: $it") },
{ println("onError: $it: ${it.message}") })
All
ReactiveX - All operator
Reactive Extensions再入門 その19「AnyメソッドとAllメソッド」
All 检查一个数据流的所有数据是否都满足某个条件。
- RxNET
var subject = new Subject<int>();
subject.Subscribe(Console.WriteLine, () => Console.WriteLine("Subject completed"));
var all = subject.All(i => i < 5);
all.Subscribe(b => Console.WriteLine("All values less than 5? {0}", b));
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(6);
subject.OnNext(2);
subject.OnNext(1);
subject.OnCompleted();
/*
1
2
6
All values less than 5? False
2
1
Subject completed
*/
- RxJava
val values = Observable.create<Int> { o ->
o.onNext(0)
o.onNext(10)
o.onNext(10)
o.onNext(2)
o.onComplete()
}
values
.all { i -> i % 2 == 0 }
.dump()
/*
onSuccess: true
*/
val values = Observable.interval(150, TimeUnit.MILLISECONDS).take(5)
val subscription = values
.all { i -> i < 3 }
.dump()
val subscription2 = values
.dump()
readLine()
subscription.dispose()
subscription2.dispose()
/*
onNext: 0
onNext: 1
onNext: 2
onNext: 3
onSuccess: false
onNext: 4
onComplete
*/
val values = Observable.create<Int> { o ->
o.onNext(0)
o.onNext(2)
o.onError(Exception())
}
values
.all { i -> i % 2 == 0 }
.dump()
/*
onError: java.lang.Exception: null
*/
- RxJS
RxJs - every
learn-rxjs - every
Amb
ReactiveX - Amb operator
Reactive Extensions再入門 その41「どんどん合成するよ」
给定两个或多个数据流,Amb 只发送最早发送数据的数据流的所有数据。
- RxNET
/*
s1 -1--1--|
s2 --2--2--|
s3 ---3--3--|
r -1--1--|
*/
var s1 = new Subject<int>();
var s2 = new Subject<int>();
var s3 = new Subject<int>();
var result = Observable.Amb(s1, s2, s3);
result.Subscribe(
Console.WriteLine,
() => Console.WriteLine("Completed"));
s1.OnNext(1);
s2.OnNext(2);
s3.OnNext(3);
s1.OnNext(1);
s2.OnNext(2);
s3.OnNext(3);
s1.OnCompleted();
s2.OnCompleted();
s3.OnCompleted();
/*
1
1
Completed
*/
/*
s1 ---1--|
s2 -2--2--|
s3 --3--3--|
r -2--2--|
*/
var s1 = new Subject<int>();
var s2 = new Subject<int>();
var s3 = new Subject<int>();
var result = Observable.Amb(s1, s2, s3);
result.Subscribe(
Console.WriteLine,
() => Console.WriteLine("Completed"));
// s1.OnNext(1);
s2.OnNext(2);
s3.OnNext(3);
s1.OnNext(1);
s2.OnNext(2);
s3.OnNext(3);
s1.OnCompleted();
s2.OnCompleted();
s3.OnCompleted();
/*
2
2
Completed
*/
- RxJava
Observable.amb(listOf(
Observable.timer(100, TimeUnit.MILLISECONDS).map { i -> "First" },
Observable.timer(50, TimeUnit.MILLISECONDS).map { i -> "Second" },
Observable.timer(70, TimeUnit.MILLISECONDS).map { i -> "Third" }))
.dump()
/*
onNext: Second
onComplete
*/
Observable.ambArray(
Observable.timer(100, TimeUnit.MILLISECONDS).map { i -> "First" },
Observable.timer(50, TimeUnit.MILLISECONDS).map { i -> "Second" },
Observable.timer(70, TimeUnit.MILLISECONDS).map { i -> "Third" })
.dump()
/*
onNext: Second
onComplete
*/
Observable.timer(100, TimeUnit.MILLISECONDS).map { i -> "First" }
.ambWith(Observable.timer(50, TimeUnit.MILLISECONDS).map { i -> "Second" })
.ambWith(Observable.timer(70, TimeUnit.MILLISECONDS).map { i -> "Third" })
.dump()
/*
onNext: Second
onComplete
*/
- RxJS
Contains / Any / Exists / IsEmpty
ReactiveX - Contains operator
Reactive Extensions再入門 その19「AnyメソッドとAllメソッド」
Contains 检查一个数据流是否发送了某个特定数据。
Any / Exists 检查一个数据流是否发送了(满足特定条件的)数据。
IsEmpty 检查一个数据流是否没有发送任何数据。
- RxNET
var subject = new Subject<int>();
subject.Subscribe(
Console.WriteLine,
() => Console.WriteLine("Subject completed"));
var contains = subject.Contains(2);
contains.Subscribe(
b => Console.WriteLine("Contains the value 2? {0}", b),
() => Console.WriteLine("contains completed"));
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
subject.OnCompleted();
/*
1
2
Contains the value 2? True
contains completed
3
Subject completed
*/
var subject = new Subject<int>();
subject.Subscribe(Console.WriteLine, () => Console.WriteLine("Subject completed"));
var any = subject.Any();
any.Subscribe(b => Console.WriteLine("The subject has any values? {0}", b));
subject.OnNext(1);
subject.OnCompleted();
/*
1
The subject has any values? True
Subject completed
*/
- RxJava
- exists 已经改名为 any
val values = Observable.interval(100, TimeUnit.MILLISECONDS)
values
.contains(4L)
.dump()
/*
onSuccess: true
*/
val values = Observable.range(0, 2)
values
.any { i -> i > 2 }
.dump()
/*
onSuccess: false
*/
val values = Observable.range(0, 2)
values
.any { i -> i > 2 }
.dump()
/*
onSuccess: false
*/
val values = Observable.timer(1000, TimeUnit.MILLISECONDS)
values
.isEmpty
.dump()
/*
onSuccess: false
*/
DefaultIfEmpty
ReactiveX - DefaultIfEmpty operator
DefaultIfEmpty 发送源数据流的所有数据,当源数据流没有数据时发送缺省的某个数据。
- RxNET
var subject = new Subject<int>();
subject.Subscribe(
Console.WriteLine,
() => Console.WriteLine("Subject completed"));
var defaultIfEmpty = subject.DefaultIfEmpty();
defaultIfEmpty.Subscribe(
b => Console.WriteLine("defaultIfEmpty value: {0}", b),
() => Console.WriteLine("defaultIfEmpty completed"));
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
subject.OnCompleted();
/*
1
defaultIfEmpty value: 1
2
defaultIfEmpty value: 2
3
defaultIfEmpty value: 3
Subject completed
defaultIfEmpty completed
*/
var subject = new Subject<int>();
subject.Subscribe(
Console.WriteLine,
() => Console.WriteLine("Subject completed"));
var defaultIfEmpty = subject.DefaultIfEmpty();
defaultIfEmpty.Subscribe(
b => Console.WriteLine("defaultIfEmpty value: {0}", b),
() => Console.WriteLine("defaultIfEmpty completed"));
var default42IfEmpty = subject.DefaultIfEmpty(42);
default42IfEmpty.Subscribe(
b => Console.WriteLine("default42IfEmpty value: {0}", b),
() => Console.WriteLine("default42IfEmpty completed"));
subject.OnCompleted();
/*
Subject completed
defaultIfEmpty value: 0
defaultIfEmpty completed
default42IfEmpty value: 42
default42IfEmpty completed
*/
- RxJava
val values = Observable.empty<Int>()
values
.defaultIfEmpty(2)
.dump()
/*
onNext: 2
onComplete
*/
val values = Observable.error<Int>(Exception())
values
.defaultIfEmpty(2)
.dump()
/*
onError: java.lang.Exception: null
*/
SequenceEqual
ReactiveX - SequenceEqual operator
SequenceEqual 判断两个数据流是否发送完全相同的数据。
- RxNET
var subject1 = new Subject<int>();
subject1.Subscribe(
i => Console.WriteLine("subject1.OnNext({0})", i),
() => Console.WriteLine("subject1 completed"));
var subject2 = new Subject<int>();
subject2.Subscribe(
i => Console.WriteLine("subject2.OnNext({0})", i),
() => Console.WriteLine("subject2 completed"));
var areEqual = subject1.SequenceEqual(subject2);
areEqual.Subscribe(
i => Console.WriteLine("areEqual.OnNext({0})", i),
() => Console.WriteLine("areEqual completed"));
subject1.OnNext(1);
subject1.OnNext(2);
subject2.OnNext(1);
subject2.OnNext(2);
subject2.OnNext(3);
subject1.OnNext(3);
subject1.OnCompleted();
subject2.OnCompleted();
/*
subject1.OnNext(1)
subject1.OnNext(2)
subject2.OnNext(1)
subject2.OnNext(2)
subject2.OnNext(3)
subject1.OnNext(3)
subject1 completed
subject2 completed
areEqual.OnNext(True)
areEqual completed
*/
- RxJava
val strings = Observable.just("1", "2", "3")
val ints = Observable.just(1, 2, 3)
Observable.sequenceEqual(strings, ints, BiPredicate { s, i -> s == i.toString() } )
.dump()
/*
onSuccess: true
*/
val strings = Observable.just("1", "2", "3")
val ints = Observable.just(1, 2, 3)
Observable.sequenceEqual(strings, ints)
.dump()
/*
onSuccess: false
*/
val values = Observable.create<Int> { o ->
o.onNext(1)
o.onNext(2)
o.onError(Exception())
}
Observable.sequenceEqual(values, values)
.dump()
/*
onError: java.lang.Exception: null
*/
SkipUntil
ReactiveX - SkipUntil operator
Reactive Extensions再入門 その8「SkipとTakeメソッド」
SkipUntil 忽略源数据流的数据,直到另一个数据流开始发送数据后才开始发送源数据流的数据。
- RxNET
var subject = new Subject<int>();
var otherSubject = new Subject<Unit>();
subject
.SkipUntil(otherSubject)
.Subscribe(Console.WriteLine, () => Console.WriteLine("Completed"));
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
otherSubject.OnNext(Unit.Default);
subject.OnNext(4);
subject.OnNext(5);
subject.OnNext(6);
subject.OnNext(7);
subject.OnNext(8);
subject.OnCompleted();
/*
4
5
6
7
8
Completed
*/
- RxJava
val values = Observable.interval(100, TimeUnit.MILLISECONDS)
val cutoff = Observable.timer(250, TimeUnit.MILLISECONDS)
val subscription = values
.skipUntil(cutoff)
.dump()
readLine()
subscription.dispose()
/*
onNext: 2
onNext: 3
onNext: 4
...
*/
- RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5, 6)
.takeWhile { $0 < 4 }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
/*
1
2
3
*/
SkipWhile
ReactiveX - SkipWhile operator
Reactive Extensions再入門 その8「SkipとTakeメソッド」
SkipWhile 忽略源数据流的数据,直到某个条件不再满足时才开始发送源数据流的数据。
- RxNET
var subject = new Subject<int>();
subject
.SkipWhile(i => i < 4)
.Subscribe(Console.WriteLine, () => Console.WriteLine("Completed"));
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
subject.OnNext(4);
subject.OnNext(3);
subject.OnNext(2);
subject.OnNext(1);
subject.OnNext(0);
subject.OnCompleted();
/*
4
3
2
1
0
Completed
*/
- RxJava
val values = Observable.interval(100, TimeUnit.MILLISECONDS)
val subscription = values
.skipWhile { v -> v < 2 }
.dump()
readLine()
subscription.dispose()
/*
onNext: 2
onNext: 3
onNext: 4
...
*/
- RxSwift
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5, 6)
.skipWhile { $0 < 4 }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
/*
4
5
6
*/
let disposeBag = DisposeBag()
Observable.of("
ReactiveX 学习笔记(6)条件操作符的更多相关文章
- python3.4学习笔记(十) 常用操作符,条件分支和循环实例
python3.4学习笔记(十) 常用操作符,条件分支和循环实例 #Pyhon常用操作符 c = d = 10 d /= 8 #3.x真正的除法 print(d) #1.25 c //= 8 #用两个 ...
- ReactiveX 学习笔记(0)学习资源
ReactiveX 学习笔记 ReactiveX 学习笔记(1) ReactiveX 学习笔记(2)创建数据流 ReactiveX 学习笔记(3)转换数据流 ReactiveX 学习笔记(4)过滤数据 ...
- SHELL学习笔记----IF条件判断,判断条件
SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外. if list then do something here ...
- Dart学习笔记-运算符-条件表达式-类型转换
Dart学习笔记-运算符-条件表达式-类型转换 一.运算符 1.算术运算符 + (加)- (减)* (乘)/ (除)~/ (取整) %(取余) 2.关系运算符 == (等等) != (不等) > ...
- python学习笔记04-了解操作符与条件分支
先来了解一下条件操作符: 运算符 描述 示例 == 检查两个操作数的值是否相等,如果是则条件变为真. 如a=3,b=3则(a == b) 为 true. != 检查两个操作数的值是否相等,如果值不相等 ...
- matlab学习笔记10_2 一般操作符
一起来学matlab-matlab学习笔记10 10_2一般操作符和数据显示格式 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德 ...
- MongoDB 学习笔记2----条件操作符
条件操作符:用于两个比较两个表达式并从mongdb中获取文档 mongodb常见的操作符及解析说明 $lt:小于 example:ago<20 $lte:小于等于 example:<=20 ...
- <c和指针>学习笔记3之操作符,表达式与指针
1 操作符 (1)移位操作符 左移<<:值最左边的几位丢弃,右边多出来的几个空位用0补齐 01101101 011(丢弃)01101000(后面三位补0) 右移>>: 算术左移 ...
- mongodb学习之:条件操作符
在前面的章节中我们已经有用到了条件操作符,这里我们再重点介绍下.MongoDB中条件操作符有: (>) 大于 - $gt (<) 小于 - $lt (>=) 大于等于 - $gte ...
- 【MongoDB学习之三】条件操作符
环境 MongoDB 4.0 CentOS6.5_x64 一.条件操作符 (>) 大于 - $gt (<) 小于 - $lt (>=) 大于等于 - $gte (<= ) 小于 ...
随机推荐
- [UE4]创建动画的3中方法
一.基于现有动画的骨骼,从头开始创建一个全新的动画.(不推荐) 选中左边的骨骼节点做旋转变化. 调整完成了别忘记点击添加“Key”以创建一个关键帧,然后再保存. 二.基于当前动画的姿势创建一个动画.( ...
- @postconstruct初始化的操作(转载)
原文地址:https://www.cnblogs.com/qingruihappy/p/7861623.html 从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注 ...
- sqlserver基本增删查语句
use StudentManageDB go insert into Students (StudentName,Gender,Birthday,Age,StudentIdNo ,PhoneNumbe ...
- C#截取字符串按字节截取SubString
public static string CutByteString(string str,int len) { string result=string.Empty;// 最终返 ...
- 旷视等Oral论文提出GeoNet:基于测地距离的点云分析深度网络
基于网格曲面的几何拓扑信息可以为物体语义分析和几何建模提供较强的线索,但是,如此重要的连接性信息在点云中是缺失的.为此,旷视西雅图研究院首次提出一种全新的深度学习网络,称之为 GeoNet,可建模点云 ...
- 用socket发送信息在浏览器上显示出来
服务端代码: import socket def main(): sock=socket.socket() sock.bind(('localhost',8089)) sock.listen(5) w ...
- IP地址查询接口API
项目需要根据ip查询pos机设备所在的省份信息,经查询有以下几种免费接口: 1. 淘宝IP API http://ip.taobao.com/service/getIpInfo.php?ip=xxx ...
- 使用sqoop从mysql导入数据到hive
目录 前言 一.使用的导入命令 二.遇到的问题及解决 1. 用文本字段进行分区的问题 2. Hadoop历史服务器Hadoop JobHistory没开启的问题 3. 连接元数据存储数据库报错 4 ...
- 使用idea创建maven多模块项目
前言 参看:http://blog.csdn.net/zht666/article/details/19040733 使用Maven管理项目时,往往需要创建多个模块,模块之间存在相互引用的关系.对于M ...
- [Unity插件]Lua行为树(十三):装饰节点完善
之前介绍了组合节点中三大常用的节点:BTSequence.BTSelector和BTParallel,一般来说,这三种就够用了,可以满足很多的需求. 接下来可以完善一下装饰节点,增加几种新的节点. 1 ...