Skip to content

distinctUntilChanged

richardszalay edited this page May 20, 2011 · 10 revisions

Emits values from the source sequence, excluding consecutive duplicates.

function distinctUntilChanged(comparer : Function = null)

comparer is function(valueA : T, valueB : T) : Boolean

OR

comparer is function(valueA : T, valueB : T) : int

Remarks

Values will be emitted from the source, but consecutive duplicates will be skipped.

If comparer is specified, it will be used to compare the two values to determine if they are duplicates. If the function returns Boolean, it should return true if the values are equal. If the function returns int, it should return 0 if the values are equal; non-zero values show inequality.

If comparer is not specified, the values will be compared using the == operator, which will work for numbers, Boolean and String, but will only compare Object references for all other types.

The returned sequence completes when the source sequence completes.

The returned sequence errors when the source sequences errors or when comparer throws an error.

Marble Diagrams

xs = source
ys = output
f(x) = comparer
"==" = values are equal
"!=" = values are not equal

xs  ──o─────o─────o─────o────/
      │     │     │     │    │
      │    f(x1) f(x2) f(x2) │
      │     !=    ==    ==   │
      │     │                │
ys  ──o─────o────────────────/

xs  ──o─────o─────o─────o────/
      │     │     │     │    │
      │    f(x1) f(x2) f(x2) │
      │    false true  true  │
      │     │                │
ys  ──o─────o────────────────/

Return Value

IObservable.<T>

Examples

var source : IObservable = Observable.fromArray([0, 0, 1, 1, 0, 1, 2, 2, 3]);

source.distinctUntilChanged()
    .subscribe(function(value : int) : void
    {
        trace(value);
    });

// Trace output is:
// 0
// 1
// 0
// 1
// 2
// 3