Skip to content

Latest commit

 

History

History
90 lines (73 loc) · 2.64 KB

scan.md

File metadata and controls

90 lines (73 loc) · 2.64 KB

Rx.Observable.prototype.scan([seed], accumulator)

Applies an accumulator function over an observable sequence and returns each intermediate result. The optional seed value is used as the initial accumulator value.

For aggregation behavior with no intermediate results, see Rx.Observable#aggregate or Rx.Observable#reduce.

Arguments

  1. [seed] (Any): The initial accumulator value.
  2. accumulator (Function): An accumulator function to be invoked on each element.

Returns

(Observable): An observable sequence which results from the comonadic bind operation.

Example

/* Without a seed */
var source = Rx.Observable.range(1, 3)
    .scan(
        function (acc, x) {
            return acc + x;
        });

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

// => Next: 1
// => Next: 3
// => Next: 6
// => Completed

/* With a seed */
var source = Rx.Observable.range(1, 3)
    .scan(
        1,
        function (acc, x) {
            return acc * x;
        });

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

// => Next: 1
// => Next: 2
// => Next: 6
// => Completed

Location

File:

Dist:

Prerequisites:

  • None

NPM Packages:

NuGet Packages:

Unit Tests: