Average

calculates the average of numbers emitted by an Observable and emits this average

The Average operator operates on an Observable that emits numbers (or items that can be evaluated as numbers), and emits a single value: the average of all of the numbers emitted by the source Observable.

See Also

Language-Specific Information:

In RxGroovy, this operator is not in the ReactiveX core, but is part of the distinct rxjava-math module, where it is implemented with four type-specific operators: averageDouble, averageFloat, averageInteger, and averageLong. The following example shows how these operators work:

Sample Code

def myObservable = Observable.create({ aSubscriber ->
  if(false == aSubscriber.isUnsubscribed()) aSubscriber.onNext(4);
  if(false == aSubscriber.isUnsubscribed()) aSubscriber.onNext(3);
  if(false == aSubscriber.isUnsubscribed()) aSubscriber.onNext(2);
  if(false == aSubscriber.isUnsubscribed()) aSubscriber.onNext(1);
  if(false == aSubscriber.isUnsubscribed()) aSubscriber.onCompleted();
});

Observable.averageInteger(myObservable).subscribe(
  { println(it); },                  // onNext
  { println("Error encountered"); }, // onError
  { println("Sequence complete"); }  // onCompleted
);
2
Sequence complete
average

You can also average not the items themselves but the results of a function applied to each item, as in the illustration above, which emits the average number of sides on the figures emitted by the source Observable.

This operator will fail with an IllegalArgumentException if the source Observable does not emit any items.

This operator is not in the RxJava core, but is part of the distinct rxjava-math module, where it is implemented with four type-specific operators: averageDouble, averageFloat, averageInteger, and averageLong.

average

You can also average not the items themselves but the results of a function applied to each item, as in the illustration above, which emits the average number of sides on the figures emitted by the source Observable.

This operator will fail with an IllegalArgumentException if the source Observable does not emit any items.

average

RxJS implements this operator as average. The following code sample shows how to use it:

Sample Code

var source = Rx.Observable.range(0, 9).average();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });
Next: 4
Completed
average

You can also average not the items themselves but the results of a function applied to each item, as in the illustration above, which emits the average number of sides on the figures emitted by the source Observable.

Sample Code

var arr = [
    { value: 1 },
    { value: 2 },
    { value: 3 }
];

var source = Rx.Observable.fromArray(arr).average(function (x) {
    return x.value;
});

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });
Next: 2
Completed

average is found in the following distributions:

  • rx.all.js
  • rx.all.compat.js
  • rx.aggregates.js

It requires one of the following:

  • rx.js
  • rx.compat.js
  • rx.lite.js
  • rx.lite.compat.js

RxPHP implements this operator as average.

Computes the average of an observable sequence of values.

Sample Code

//from https://github.com/ReactiveX/RxPHP/blob/master/demo/average/average.php

$source = Rx\Observable::range(0, 9)->average();

$subscription = $source->subscribe($stdoutObserver);

   
Next value: 4
Complete!