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

Determines if all value in the sequences matches against predicate

function all(predicate : Function) : IObservable.<Boolean>

predicate is function(value : T) : Boolean

Remarks

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

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

The returned sequence completes either when predicate returns false 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)
     true  true  true false
                        │
ys  ────────────────────o/
                      false

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

Return Value

IObservable.<Boolean>

Examples

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

source.all(function(i : int) : Boolean { return i < 5; })
    .subscribe(function(all : Boolean) : void
    {
        if (all)
        {
            trace("The range contained values that are all < 5");
        }
        else
        {
            trace("The range contained a value that was >= 5");
        }
    });

// Trace output is:
// The range contained a value that was >= 5