RxPY - 条件和布尔运算符
-
all
该运算符将检查源 observable 中的所有值是否满足给定条件。句法
all(predicate)
参数
predicate:布尔值。此函数将应用于源 observable 中的所有值,并将根据给定的条件返回 true 或 false。返回值
返回值是一个 observable,它的布尔值 true 或 false,取决于应用于源 observable 的所有值的条件。示例 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 的值,则此运算符将返回值为 true 或 false 的 observable。句法
contains(value, comparer=None)
参数
value:要检查的值是否存在于源 observable 中comparer:可选。这是一个比较器函数,用于比较源 observable 中存在的值。Examplefrom 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
如果源 observable 为空,此运算符将返回默认值。句法
default_if_empty(default_value=None)
参数
default_value:可选。它将给出输出,因为 None 没有作为 default_value 传递,否则它将给出传递的任何值。返回值
如果源 observable 为空,它将返回一个带有默认值的 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:通过 default_value
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
此运算符将比较两个可观察对象序列或值数组,并返回值为真或假的可观察对象。句法
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
该运算符将丢弃源 observable 中的值,直到第二个 observable 发出值。句法
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
-
skip_while
该操作符将返回一个 observable,其值来自满足传递条件的源 observable。句法
skip_while(predicate_func)
参数
predicate_func:该函数将作用于源 observable 的所有值,并返回满足条件的值。返回值
它将返回一个 observable,其中包含满足传递条件的源 observable 的值。例子
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
在第二个 observable 发出值或终止后,此运算符将丢弃源 observable 中的值。句法
take_until(observable)
参数
observable:第二个 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)))
在此示例中,您将获得从范围发出的值。但是,一旦计时器完成,它将阻止可观察源进一步发射。输出
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:此函数将评估源 observable 的每个值。返回值
它将返回一个带有值的可观察对象,直到谓词函数满足为止。例子
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