RxPY – Filter Operator
RxPY – Filter Operators
debounce
This operator will return the values of the source observable until a given time range, ignoring the remaining values if the time has passed.
Syntax
debounce(duetime)
Parameters
duetime: This will be the value in seconds or time instances that will determine the value returned from the source observable.
Example
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)))
Output
E:pyrx>python testrx.py
The value is 10
distinct
This operator returns all values that are distinct from the source observable.
Syntax
distinct()
Return Value
It returns an observable that contains all values that are distinct from the source observable.
Example
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)))
Output
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
This operator returns an element at an index from the source observable.
Syntax
element_at(index)
Parameters
index: The zero-based number at which you want to get the element from the source observable.
Return Value
It returns an observable with the value from the source observable, given the index.
Example
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)))
Output
E:pyrx>python testrx.py
The value is 6
filter
This operator will filter values from the source observer based on the given predicate function.
Syntax
filter(predicate_func)
Parameters
predicate_func: This function determines the values to be filtered from the source observer.
Return Value
Returns an observer with the filtered values of the source observer based on the predicate function.
Example
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)))
In this example, we have filtered out all even numbers.
Output
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
This operator returns the first element of the source observation.
Syntax
first(predicate_func=None)
Parameters
predicate_func: (Optional) This function will determine the first element to be selected based on the passed condition.
Return Value
It returns an observation variable with the first value of the source observation variable.
Example
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)))
Output
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)))
Output
E:pyrx>python test1.py
The first element is 6
ignore_elements
This operator ignores all values from the source Observable and only calls the completion or error callback.
Syntax
ignore_elements()
Return Value
This returns an observer variable that will be called upon completion or error based on the source observer variable.
Example
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!"))
Output
E:pyrx>python testrx.py
Job Done!
last
This operator returns the last element of the source observer.
Syntax
last(predicate_func=None)
Parameters
predicate_func: (Optional) This function determines the last element to be selected based on the passed predicate.
Return Value
Returns an observer with the last value of the source observer.
Example
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)))
Output
E:pyrx>python test1.py
The last element is 40
skip
This operator returns an observable that skips the first occurrence of the counted item given as input.
Syntax
skip(count)
Parameters
Count: Count is the number of items to skip from the source observer.
Return Value
This returns an observable with the skipped values based on the given count.
Example
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)))
Output
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
This operator returns an observable that skips the last occurrence of the counted item given as input.
Syntax
skip_last(count)
Parameters
Count: Count is the number of items to skip from the source observer.
Return Value
This returns an observable with the skip value based on the last given count.
Example
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)))
Output
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
This operator takes a list of source values in consecutive order, given a given count.
Syntax
take(count)
Parameters
Count: Count is the number of items to be taken from the source observable.
Return Value
It returns an observable with values in sequential order based on the given count.
Example
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)))
Output
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
This operator takes a list of source values, sorted consecutively starting with the last one, based on the given count.
Syntax
take_last(count)
Parameters
Count: Count is the number of items to be taken from the source observable.
Return Value
It returns an observable whose values are sorted sequentially starting from the last one according to the given count.
Example
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)))
Output
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