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

Takes values from a source sequence until a condition is no longer met

function takeWhile(predicate : Function) : IObservable.<T>

predicate is function(value : T) : Boolean

Remarks

Each value emitted by the source will be sent to predicate. If predicate returns true, the value will be emitted; otherwise the value will be skipped and the sequence will complete.

The returned sequence completes when the source sequence completes or predicate returns false

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

Marble Diagrams

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

xs  ──o─────o─────o
      │     │     │
     p(x)  p(x)  p(x)
     true  true false
      │     │     │
ys  ──o─────o─────/

Return Value

IObservable.<sourceType>

Examples

var source : IObservable = Observable.range(0, 10)
    .takeWhile(function(v:int) : Boolean { return v < 5; });

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

// Trace output is:
// 0
// 1
// 2
// 3
// 4
Clone this wiki locally