RxPY - 条件和布尔运算符



全部

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

语法

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

如果给定值存在于源可观察对象的值中,则此运算符将返回一个具有 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,否则它将输出传递的任何值。

返回值

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

示例 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

此运算符将比较两个可观察对象的序列或值数组,并返回一个具有 true 或 false 值的可观察对象。

语法

sequence_equal(second_seq, comparer=None)

参数

second_seq: 要与第一个可观察对象进行比较的可观察对象或数组。

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: 当发出值时将触发源可观察对象的第二个可观察对象。

返回值

它将返回一个可观察对象,该可观察对象将具有来自源可观察对象的值,直到第二个可观察对象发出值。

示例

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

此运算符将返回一个可观察对象,其中包含来自满足传递条件的源可观察对象的值。

语法

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 发出的值。但是,一旦计时器完成,它将停止源可观察对象进一步发出值。

输出

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

此运算符将在条件失败时丢弃来自源可观察对象的值。

语法

take_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.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
广告

© . All rights reserved.