RxPY – Mathematical Operators

RxPY – Mathematical Operators

average

This operator calculates the average from the given source observables and outputs an observable with the average.

Syntax

average()

Return Value

It returns an observable with the average.

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 takes two or more observables and produces a single observable with all the values in the sequence.

Syntax

concat(observable1, observable2...)

Parameters

Observables: A list of observables to concatenate.

Return Value

Returns a single observable with the values of the source observables concatenated.

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 an observable with a value and transforms it into an observable with a single value. The count function takes a predicate function as an optional argument. This function is of type Boolean and adds a value to the output only if the condition is met.

Syntax

count(predicate_function=None)

Parameters

The count function takes a predicate function as an optional argument. This function is of Boolean type and adds a value to the output only if the condition is met.

Return Value

It returns an observation variable with a single value: the count from the source observation variable.

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 returns an observation with the maximum value of the source observations.

Syntax

max(comparer_function=None)

Parameters

comparer_function: Optional parameter. This function is applied to the source observations to compare the values.

Return Value

This returns an observation variable with the maximum value of the source observation variable.

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: Comparator_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 returns an observation with the minimum value from the source observations.

Syntax

min(comparer_function=None)

Parameters

comparer_function: Optional parameter. This function is used on the source observations to compare values.

Return Value

It returns an observation with the minimum value of the source observations.

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 comparator_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 a function called an accumulator function, which is applied to the values from the source observer. It returns the accumulated value as an observer, with an optional seed value passed to the accumulator function.

Syntax

reduce(accumulator_func, seed=notset)

Parameters

accumulator_func: A function that processes values from the source observable and returns the accumulated value as an observable.

seed: Optional. The default value is not set. This is the initial value used by the accumulator function.

Return Value

Returns an observable with a single value as the output of the accumulator function, applied to 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 returns an observation variable containing the sum of all values from the source observation variable.

Syntax

sum(key_mapper=none)

Parameters

key_mapper: Optional. This is a function that is applied to the values from the source observations.

Return Value

Returns an observation that contains the sum of all the values from the source observations.

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 the key_mapper function, we add 1 to all values and calculate their sum.

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

Leave a Reply

Your email address will not be published. Required fields are marked *