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

Creates a custom observable sequence that is iterates using callback functions.

static function generate(initialState : Object, predicate : Function, 
    iterate : Function, resultMap : Function, scheduler : IScheduler = null) : IObservable.<TResult>

Where predicate is function(state : stateClass) : Boolean

Where iterate is function(state : stateClass) : stateClass

Where resultMap is function(state : stateClass) : TResult

Remarks

Starting with initialState, values go through the following cycle:

  1. The current state is passed to predicate
    • If predicate returns false, the sequence completes
  2. The current state is passed to resultMap and the return value is emitted as a value
  3. The current state is passed to iterate and the return value replaces the current state

The returned sequence completes when predicate returns false

The returned sequence raises an error if iterate, predicate or resultMap throw an error.

Marble Diagrams

p = predicate
map = resultMap
it = iterate
s = state

state  ────p(s)──it(s)──p(s)──it(s)──p(s)
           true         true         false
            │            │            │
          map(s)       map(s)         │
            │            │            │
output ─────o────────────o────────────/

Scheduling

Unless specified, this operator uses Scheduler.synchronous.

Return Value

IObservable.<valueClass>

Examples

Observable.generate(5,
        function(state:int):Boolean { return state <= 20; },
        function(state:int):int { return state + 5; },
        function(state:int):String { return state.toString() + "!"; }
    )
    .subscribe(
        function(value : int) : void { trace(value); },
        function():void { trace("Completed"); }
    );

    // Trace output is:
    // 5!
    // 10!
    // 15!
    // 20!
    // Completed