RxPY – 组合运算符
combine_latest
这个操作符将创建一个元组,用于作为输入的可观测变量。
语法
combine_latest(observable1,observable2,.....)
参数
可观察的:一个可观察变量。
返回值
它返回一个可观察变量,并将源观察变量的值转换为一个元组。
例子
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
test2 = of(11,12,13,14,15,16)
test3 = of(111,112,113,114,115,116)
sub1 = test.pipe(
op.combine_latest(test2, test3)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py
The value is (6, 16, 111)
The value is (6, 16, 112)
The value is (6, 16, 113)
The value is (6, 16, 114)
The value is (6, 16, 115)
The value is (6, 16, 116)
merge
这个操作符将合并给定的观察变量。
语法
merge(observable)
参数
观察者:一个可观察者。
返回值
它将从给定的观察变量中返回一个带有一个序列的观察变量。
例子
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
test2 = of(11,12,13,14,15,16)
sub1 = test.pipe(
op.merge(test2)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py
The value is 1
The value is 2
The value is 3
The value is 4
The value is 5
The value is 6
The value is 11
The value is 12
The value is 13
The value is 14
The value is 15
The value is 16
start_with
这个操作符将接收给定的值,并在源观测器的开始处添加,然后返回完整的序列。
语法
start_with(values)
参数
值:你想在开始时加前缀的值。
返回值
它返回一个在开始时给定值加了前缀的可观察变量,后面是源观察变量的值。
例子
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
sub1 = test.pipe(
op.start_with(-2,-1,0)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))xExample
输出
E:\pyrx>python testrx.py
The value is -2
The value is -1
The value is 0
The value is 1
The value is 2
The value is 3
The value is 4
The value is 5
The value is 6
zip
这个操作符返回一个带有元组形式的值的可观察变量,它是由给定的可观察变量的第一个值组成的,以此类推。
语法
zip(observable1, observable2...)
参数
观察者:一个可观察者
返回值
它返回一个具有元组格式值的可观察变量。
例子
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
test1 = of(4,8,12,16,20)
test2 = of(5,10,15,20,25)
sub1 = test.pipe(
op.zip(test1, test2)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
输出
E:\pyrx>python testrx.py
The value is (1, 4, 5)
The value is (2, 8, 10)
The value is (3, 12, 15)
The value is (4, 16, 20)
The value is (5, 20, 25)