RxPY 条件和布尔运算符

RxPY 条件和布尔运算符

all

此运算符将检查源可观察对象的所有值是否满足给定的条件。

语法

all(predicate)

参数

predicate: 布尔值。该函数将应用于源可观测对象的所有值,并根据给定的条件返回true或false。

返回值

返回值是一个可观测对象,其布尔值为true或false,取决于应用于源可观测对象的所有值的条件。

示例1

from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
   op.all(lambda a: a<10)
)
sub1.subscribe(lambda x: print("The result is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The result is False

示例2

from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9)
sub1 = test.pipe(
   op.all(lambda a: a<10)
)
sub1.subscribe(lambda x: print("The result is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The result is True

contains

该操作符将返回一个Observable,如果给定的值存在于源Observable的值中,则返回true;如果不存在,则返回false。

语法

contains(value, comparer=None)

参数

value: 要检查的值是否存在于源可观察对象中

comparer: 可选项。这是一个应用于源可观察对象中的值进行比较的比较器函数。

示例

from rx import of, operators as op
test = of(17, 25, 34, 56, 78)
sub1 = test.pipe(
   op.contains(34)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The value is True

示例2:使用比较器

from rx import of, operators as op
test = of(17, 25, 34, 56, 78)
sub1 = test.pipe(
   op.contains(34, lambda x, y: x == y)
)
sub1.subscribe(lambda x: print("The valus is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The value is True

default_if_empty

如果源可观测对象为空,则此运算符将返回一个默认值。

语法

default_if_empty(default_value=None)

参数

default_value:可选参数。如果default_value未传入,将返回None作为输出;否则将返回传入的值。

返回值

如果源observable为空,将返回一个带有默认值的可观察对象。

示例1

from rx import of, operators as op
test = of()
sub1 = test.pipe(
   op.default_if_empty()
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The value is None

示例2:传入了默认值

from rx import of, operators as op
test = of()
sub1 = test.pipe(
   op.default_if_empty("Empty!")
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The value is Empty!

sequence_equal

此运算符将比较两个 observables 序列,或一个值数组,并返回一个具有 true 或 false 值的 observable。

语法

sequence_equal(second_seq, comparer=None)

参数

second_seq: 要与第一个observable比较的observable或数组。

comparer: 可选的。用于比较两个序列中的值的比较函数。

示例

from rx import of, operators as op
test = of(1,2,3)
test1 = of(1,2,3)
sub1 = test.pipe(
   op.sequence_equal(test1)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The value is True

示例:使用比较函数

from rx import of, operators as op
test = of(1,2,3)
test1 = of(1,2,3)
sub1 = test.pipe(
   op.sequence_equal(test1, lambda x, y : x == y)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The value is True

skip_until

此运算符将从源可观察对象中丢弃值,直到第二个可观察对象发出一个值为止。

语法

skip_until(observable)

参数

observable:当它发出一个值时将触发源observable的第二个observable。

返回值

它将返回一个observable,该observable将具有来自源observable的值,直到第二个observable发出一个值。

示例

from rx import interval,range, operators as op
from datetime import date
test = interval(0)
test1 = range(10)
sub1 = test1.pipe(
   op.skip_until(test)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

E:\pyrx>python testrx.py
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
The value is 7
The value is 8
The value is 9

跳过条件满足的observable值

此操作符将返回一个observable,其中包含满足传递条件的源observable的值。

语法

skip_while(predicate_func)

参数

predicate_func: 该函数将应用于源可观测对象的所有值,并返回满足条件的值。

返回值

它将返回一个可观测对象,其值来自满足传入条件的源可观测对象。

示例

from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6,7,8,9,10)
sub1 = test.pipe(
   op.skip_while(lambda x : x < 5)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The value is 5
The value is 6
The value is 7
The value is 8
The value is 9
The value is 10

take_until

此操作符将在第二个可观察对象发出一个值或终止后,从源可观察对象中丢弃值。

语法

take_until(observable)

参数

observable:第二个可观察对象,当其发出一个值时将终止源可观察对象。

返回值

返回一个可观察对象,该对象仅在第二个可观察对象发出值时才包含源可观察对象的值。

示例

from rx import timer,range, operators as op
from datetime import date
test = timer(0.01)
test1 = range(500)
sub1 = test1.pipe(
   op.take_until(test)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

在这个示例中,你将获取从range发出的值。但是,一旦计时器结束,它将停止源Observable继续发出。

输出

E:\pyrx>python testrx.py
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
The value is 7
The value is 8
The value is 9
The value is 10
The value is 11
The value is 12
The value is 13
The value is 14
The value is 15
The value is 16
The value is 17
The value is 18
The value is 19
The value is 20
The value is 21
The value is 22
The value is 23
The value is 24
The value is 25
The value is 26

take_while

此操作符在条件失败时,将从源observable中丢弃值。

语法

take_while(predicate_func)

参数

predicate_func:该函数将评估源可观测对象的每个值。

返回值

它将返回一个包含值的可观测对象,直到满足predicate函数。

示例

from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6,7,8,9,10)
sub1 = test.pipe(
   op.take_while(lambda a : a < 5)
)
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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程