RxPY - Mathematical Operators



average

This operator will calculate the average from the source observable given and output an observable that will have the average value.

Syntax

average()

Return value

It returns an observable that will have the average value.

Example

from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
   op.average()
)
sub1.subscribe(lambda x: print("Average is {0}".format(x)))

Output

E:\pyrx>python testrx.py
Average is 5.5

concat

This operator will take in two or more observables and give a single observable with all the values in the sequence.

Syntax

concat(observable1, observable2...)

Parameters

Observables: List of observables to be concatenated.

Return value

An observable is returned with a single value merged from the values of the source observable.

Example

testrx.py

from rx import of, operators as op
test = of(2, 4, 6, 8, 10)
test2 = of(3,6,9,12,15)
sub1 = test.pipe(
   op.concat(test2)
)
sub1.subscribe(lambda x: print("Final value is {0}".format(x)))

Output

E:\pyrx>python testrx.py
Final value is 2
Final value is 4
Final value is 6
Final value is 8
Final value is 10
Final value is 3
Final value is 6
Final value is 9
Final value is 12
Final value is 15

count

This operator takes in an observable with values, and converts it into an observable that will have a single value. The count function takes in predicate function as an optional argument. The function is of type Boolean, and will add value to the output only, if it satisfies the condition.

Syntax

count(predicate_function=None)

Parameters

The count function takes in predicate function as an optional argument. The function is of type Boolean, and will add value to the output only, if it satisfies the condition.

Return value

It will return an observable with a single value, i.e. the count from the source observable.

Example 1

from rx import of, operators as op
test = of(1,2,3, 4,5, 6,7, 8,9, 10)
sub1 = test.pipe(
   op.count()
)
sub1.subscribe(lambda x: print("The count is {0}".format(x)))

Output

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

Example 2: Using a predicate function

from rx import of, operators as op
test = of(1,2,3, 4,5, 6,7, 8,9, 10)
sub1 = test.pipe(
   op.count(lambda x : x %2 == 0)
)
sub1.subscribe(lambda x: print("The count of even numbers is {0}".format(x)))

Output

E:\pyrx>python testrx.py
The count of even numbers is 5

max

This operator will give an observable with max value from the source observable.

Syntax

max(comparer_function=None)

Parameters

comparer_function: optional param. This function is used on source observables to compare values.

Return value

It returns an observable with max value from the source observable.

Example 1

from rx import of, operators as op
test = of(12,32,41,50,280,250)
sub1 = test.pipe(
   op.max()
)
sub1.subscribe(lambda x: print("Max value is {0}".format(x)))

Output

E:\pyrx>python testrx.py
Max value is 280

Example 2: comparer_function

from rx import of, operators as op
test = of(12,32,41,50,280,250)
sub1 = test.pipe(
   op.max(lambda a, b : a - b)
)
sub1.subscribe(lambda x: print("Max value is {0}".format(x)))

Output

E:\pyrx>python testrx.py
Max value is 280

min

This operator will give an observable with min value from the source observable.

Syntax

min(comparer_function=None)

Parameters

comparer_function: optional param. This function is used on source observables to compare values.

Return value

It returns an observable with min value from the source observable.

Example 1

from rx import of, operators as op
test = of(12,32,41,50,280,250)
sub1 = test.pipe(
   op.min()
)
sub1.subscribe(lambda x: print("Min value is {0}".format(x)))

Output

E:\pyrx>python testrx.py
Min value is 12

Example 2: Using comparer_function

from rx import of, operators as op
test = of(12,32,41,50,280,250)
sub1 = test.pipe(
   op.min(lambda a, b : a - b)
)
sub1.subscribe(lambda x: print("Min value is {0}".format(x)))

Output

E:\pyrx>python testrx.py
Min value is 12

reduce

This operator takes in a function called accumulator function, that is used on the values coming from the source observable, and it returns the accumulated values in the form of an observable, with an optional seed value passed to the accumulator function.

Syntax

reduce(accumulator_func, seed=notset)

Parameters

accumulator_func: A function that is used on the values coming from the source observable, and it returns the accumulated values in the form of an observable.

seed: optional. The default value is not set. It is the initial value, to be used inside the accumulator function.

Return value

It returns an observable, with a single value as output from the accumulator function applied on each value of the source observable.

Example

from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
   op.reduce(lambda acc, x: acc + x)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))

Output

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

sum

This operator will return an observable with the sum of all the values from source observables.

Syntax

sum(key_mapper=none)

Parameters

key_mapper: optional. This is the function, that is applied to the values coming from the source observable.

Return value

It returns an observable with the sum of all the values from the source observable.

Example 1

from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
   op.sum()
)
sub1.subscribe(lambda x: print("The sum is {0}".format(x)))

Output

E:\pyrx>python testrx.py
The sum is 55

Example 2: using key_mapper function

from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
   op.sum(lambda a: a+1)
)
sub1.subscribe(lambda x: print("The sum is {0}".format(x)))

Using key_mapper function, we are adding all the values by 1 and getting the sum of it.

E:\pyrx>python testrx.py
The sum is 65
Advertisements