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.