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

Determines if all a sequence contains a specific value

static function contains(value : TValue, comparer : Function = null)

value is the value to compare

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

OR

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

Remarks

value will be compared against each value that comes from the source. If the values are equal, the value true will be emitted and the sequence will immediately complete (unsubscribing from the source).

If the source sequence completes without a matching value (including an empty source sequence), the value false will be emitted and the sequence will immediately complete.

If comparer is specified, it will be used to compare the two values. 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 either when comparer returns true or 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(x)  f(x)  f(x)  f(x)
     false false false true
                        │
ys  ────────────────────o/
                      true

xs  ──o─────o─────o─────/
      │     │     │     │
     f(x)  f(x)  f(x)   │
     false false false  │
                        │
ys  ────────────────────o/
                      false

xs  ──o─────o─────o─────o
      │     │     │     │
      !=    !=    !=    ==
                        │
ys  ────────────────────o/
                      true

xs  ──o─────o─────o─────/
      │     │     │     │
      !=    !=    !=    │
                        │
ys  ────────────────────o/
                      false

Return Value

IObservable.<Boolean>

Examples

var source : IObservable = Observable.range(0, 10);

source.contains("5", function(a : int, b : int) : Boolean
    {
        return a.toString() == b.toString();
    })
    .subscribe(function(contained : Boolean) : void
    {
        if (contained)
        {
            trace("The range contained a value that had a toString value of '5'");
        }
        else
        {
            trace("The range did not contain a value '5'");
        }
    });

// Trace output is:
// The range contained a value that had a toString value of '5'
var source : IObservable = getObservableListOfPeople();

var personToFind : Person = new Person("John", "Citizen");

source
    .contains(personToFind, function(a : Person, b : Person) : Boolean
    {
        return a.firstName == b.firstName &&
            a.lastName == b.lastName;
    })
    .subscribe(function(contained : Boolean) : void
    {
        if (contained)
        {
            trace("John Citizen was found in the source");
        }
        else
        {
            trace("John Citizen was not found in the source");
        }
    });
Clone this wiki locally