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

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

function sum(): IObservable.<Number>

Remarks

If there are no values in the source sequence, the sequence will emit 0.

The returned sequence completes when the source sequence completes.

The returned sequence raises an error if the source sequence raises an error.

Marble Diagrams

xs = source
zs = output

xs ───o───o───o───o─/
      2 + 4 + 6 + 8 │
                    │
zs ─────────────────o/
                   20

xs ───────────────/
                  │
                  │
zs ───────────────o/
                  0

Return Value

IObservable.<Number>

Examples

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

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

    // Trace output is:
    // 0
    // Completed
Clone this wiki locally