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 (<= ) 小于 ...
随机推荐
- typescript函数类型接口
/* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据, ...
- 2017.7月(关于vertical-align等)
1.Jquery 可以使用$('.class',$object)的形式,$object是jQuery对象,在该对象的子元素中查找'.class' 2.line的问题,input和button作为inl ...
- sas基础系列(3)-表格标颜色示例
以下代码可以直接在SAS执行查看效果 ods path reset;ods path show;ods html close;options nodate;ods pdf file="Pro ...
- Spring MVC 处理JSON | JSONP类型数据
SpringMVC返回JSON格式的数据: 1 添加jar包(gson-2.8.0.jar): <dependency> <groupId>com.google.code.gs ...
- Opening socket connection to server :2181. Will not attempt to authenticate using SASL (unknown error) hbase
问题: 在HBase机群搭建完成后,通过jdbc连接hbase,在连接zookeeper阶段出现Opening socket connection to server :2181. Will not ...
- SPOJ COT3 - Combat on a tree
/* 考虑直接使用暴力来算的话 SG[i]表示以i为根的子树的SG值, 然后考虑枚举删除那个子树节点, 然后求拆成的树的sg异或值, 求mex即可 复杂度三次方 然后考虑尝试 整体来做 发现对于每次子 ...
- mysql where in形式存储过程如何传递带有单引号的入参
对于存储过程或者函数,我们通常都有这样的一个需求,传递一个参数,输出一个结果.如:我传递一个账号,返回这个账号所拥有的权限.但是如果这个需求改了,我要传递多个账号,获取这些账号权限,但是账号的个数不限 ...
- 【Jmeter自学】Linux环境下Jmeter运行
==================================================================================================== ...
- tree-data
[{ label: '一级 1', children: [{ label: '二级 1-1', children: [{ label: '三级 1-1-1' }] }] }, { label: '一级 ...
- HANA Database SR Basis Setting
HANA Database SR Basis Setting: 1.关闭Hana数据库,把System replication专用的IP地址和相关主机名填写到global.ini配置文件里: #su ...