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

Determines if any value in the sequences matches against predicate

function any(predicate : Function = null) : IObservable.<Boolean>

If predicate is not specified, the sequence will emit true if the source sequence contains any values

predicate is function(value : T) : Boolean

Remarks

predicate will be called for each value that comes from the source. If predicate returns true, the value true will be emitted and the sequence will immediately complete (unsubscribing from the source).

If the source sequence completes without predicate ever returning true (including an empty source sequence), the value false will be emitted and the sequence will immediately complete.

The returned sequence completes either when predicate returns true or when the source sequence completes.

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

Marble Diagrams

xs = source
ys = output
f(x) = predicate

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

Return Value

IObservable.<Boolean>

Examples

Observable.fromEvent(stage, KeyboardEvent.KEY_DOWN)
    .take(5)
    .any(function(event : KeyboardEvent) : Boolean { return event.altKey; })
    .subscribe(function(any : Boolean) : void
    {
        if (any)
        {
            trace("You pressed the alt key");
        }
        else
        {
            trace("You pressed 5 keys without pressing alt");
        }
    });
var source : IObservable = Observable.range(0, 10);

source.any(function(i : int) : Boolean { return i > 5; })
    .subscribe(function(any : Boolean) : void
    {
        if (any)
        {
            trace("The range contains a value that is > 5");
        }
        else
        {
            trace("The range does not contain a value that is > 5");
        }
    });

// Trace output is:
// The range contains a value that is > 5
var source : IObservable = Observable.range(0, 10);

source.any()
    .subscribe(function(any : Boolean) : void
    {
        if (any)
        {
            trace("The contains a value");
        }
        else
        {
            trace("The source does not contain a value");
        }
    });

// Trace output is:
// The contains a value