RxPY - 过滤运算符

debounce

此运算符将给出源可观察值的值,直到给定的时间跨度,如果时间过去,则忽略其余值。

语法

debounce(duetime)

参数

duetime: 这将以秒或时间实例为单位,持续时间将决定要从源可观察对象返回的值。

示例

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.debounce(2.0)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

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

distinct

此运算符将提供与源可观察对象不同的所有值。

语法

distinct()

返回值

它将返回一个 observable,其中它将具有与源 observable 不同的值。

示例

from rx import of, operators as op
from datetime import date
test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40)
sub1 = test.pipe(
   op.distinct()
)
sub1.subscribe(lambda x: print("The distinct value is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The distinct value is 1
The distinct value is 6
The distinct value is 15
The distinct value is 10
The distinct value is 40
The distinct value is 58
The distinct value is 20

element_at

此运算符将为给定索引提供源可观察到的元素。

语法

element_at(index)

参数

index: 从零开始的数字,您需要来自可观察源的元素。

返回值

它将返回一个具有给定索引的源可观察值的可观察值。

示例

from rx import of, operators as op
from datetime import date
test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40)
sub1 = test.pipe(
   op.element_at(5)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

输出

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

filter

此运算符将根据给定的谓词函数从源可观察对象中过滤值。

语法

filter(predicate_func)

参数

predicate_func: 此函数将决定要从源可观察对象中过滤的值。

返回值

它将返回一个 observable,该 observable 将具有基于谓词函数从源 observable 过滤的值。

示例

from rx import of, operators as op
from datetime import date
test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40)
sub1 = test.pipe(
   op.filter(lambda x : x %2==0)
)
sub1.subscribe(lambda x: print("The filtered value is {0}".format(x)))

在示例中,我们过滤了所有偶数。

输出

E:\pyrx>python testrx.py
The filtered value is 6
The filtered value is 10
The filtered value is 6
The filtered value is 40
The filtered value is 10
The filtered value is 58
The filtered value is 20
The filtered value is 40

first

此运算符将给出源可观察对象的第一个元素。

语法

first(predicate_func=None)

参数

predicate_func: (可选)如果通过,该函数将根据条件决定第一个被拾取的元素。

返回值

它将返回一个带有来自源可观察对象的第一个值的可观察对象。

示例

from rx import of, operators as op
from datetime import date
test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40)
sub1 = test.pipe(
   op.first()
)
sub1.subscribe(lambda x: print("The first element is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The first element is 1

Example 2: using predicate_func

from rx import of, operators as op
from datetime import date
test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40)
sub1 = test.pipe(
   op.first(lambda x : x%2==0)
)
sub1.subscribe(lambda x: print("The first element is {0}".format(x)))

输出

E:\pyrx>python test1.py
The first element is 6

ignore_elements

此运算符将忽略来自源 Observable 的所有值,并且仅执行调用以完成或错误回调函数。

语法

ignore_elements()

返回值

它返回一个 observable,它将根据源 observable 调用 complete 或 error。

示例

from rx import of, operators as op
from datetime import date
test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40)
sub1 = test.pipe(
   op.ignore_elements()
)
sub1.subscribe(lambda x: print("The first element is {0}".format(x)),
lambda e: print("Error : {0}".format(e)),
lambda: print("Job Done!"))

输出

E:\pyrx>python testrx.py
Job Done!

last

此运算符将给出源可观察对象的最后一个元素。

语法

last(predicate_func=None)

参数

predicate_func: (可选)如果通过,此函数将根据条件决定要选择的最后一个元素。

返回值

它将返回一个带有来自源可观察对象的最后一个值的可观察对象。

示例

from rx import of, operators as op
from datetime import date
test = of(1, 6, 15, 1, 10, 6, 40, 10, 58, 20, 40)
sub1 = test.pipe(
   op.last()
)
sub1.subscribe(lambda x: print("The last element is {0}".format(x)))

输出

E:\pyrx>python test1.py
The last element is 40

skip

此运算符将返回一个可观察对象,它将跳过第一次出现的作为输入的计数项。

语法

skip(count)

参数

count: 该计数是项目将从源可观察对象中跳过的次数。

返回值

它将返回一个根据给定计数跳过值的可观察对象。

示例

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(5)
)
sub1.subscribe(lambda x: print("The element is {0}".format(x)))

输出

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

skip_last

此运算符将返回一个可观察对象,它将跳过作为输入的最后一次出现的计数项。

语法

skip_last(count)

参数

count: 计数是项目将从可观察源中跳过的次数。

返回值

它将返回一个可观察值,该可观察值会根据上次给出的计数跳过值。

示例

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_last(5)
)
sub1.subscribe(lambda x: print("The element is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The element is 1
The element is 2
The element is 3
The element is 4
The element is 5

take

该运算符将根据给定的计数按连续顺序给出源值列表。

语法

take(count)

参数

count: 计数是项目的数量,将从可观察的源中给出。

返回值

它将返回一个具有基于给定计数的连续顺序值的可观察值。

示例

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(5)
)
sub1.subscribe(lambda x: print("The element is {0}".format(x)))

输出

E:\pyrx>python testrx.py
The element is 1
The element is 2
The element is 3
The element is 4
The element is 5

take_last

此运算符将根据给定的计数从最后一个连续顺序给出源值列表。

语法

take_last(count)

参数

count: 计数是项目的数量,将从可观察的源中给出。

返回值

它将返回一个 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_last(5)
)
sub1.subscribe(lambda x: print("The element is {0}".format(x)))

输出

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