Skip to content
richardszalay edited this page May 20, 2011 · 9 revisions

Returns the average value of all the elements in the source sequence

static function average(): IObservable.<Number>

Remarks

The returned sequence completes when the source sequence completes.

The returned sequence raises an error if the source sequence raises an error or if the source sequence is empty.

Marble Diagrams

xs = source
ys = output

xs ───o───o───o───o─/
      2   4   6   8 │
                    │
ys ─────────────────o/
                    5

xs ───────────────/
                  │
                  │
ys ───────────────x

Return Value

@IObservable.

Examples

Observable.range(1, 5)
    .average()
    .subscribe(
        function(value : int) : void { trace(value); },
        function():void { trace("Completed"); }
    );

    // Trace output is:
    // 3
    // Completed
Observable.empty()
    .sum()
    .subscribe(
        function(value : int) : void { trace(value); },
        function():void { trace("Completed"); },
        function(e:Error) : void { trace(e.message); }
    );

    // Trace output is:
    // Sequence contained no elements
Clone this wiki locally