Export (0) Print
Expand All

Observable.Generate<TState, TResult> Method (TState, Func<TState, Boolean>, Func<TState, TState>, Func<TState, TResult>, Func<TState, TimeSpan>, IScheduler)

Generates an observable sequence by iterating a state from an initial state until the condition fails.

Namespace:  System.Reactive.Linq
Assembly:  System.Reactive (in System.Reactive.dll)

public static IObservable<TResult> Generate<TState, TResult>(
	TState initialState,
	Func<TState, bool> condition,
	Func<TState, TState> iterate,
	Func<TState, TResult> resultSelector,
	Func<TState, TimeSpan> timeSelector,
	IScheduler scheduler
)

Type Parameters

TState

The type of state.

TResult

The type of result.

Parameters

initialState
Type: TState
The initial state.
condition
Type: System.Func<TState, Boolean>
The condition to terminate generation.
iterate
Type: System.Func<TState, TState>
The iteration step function.
resultSelector
Type: System.Func<TState, TResult>
The selector function for results produced in the sequence.
timeSelector
Type: System.Func<TState, TimeSpan>
The time selector function to control the speed of values being produced each iteration.
scheduler
Type: System.Reactive.Concurrency.IScheduler
The scheduler on which to run the generator loop.

Return Value

Type: System.IObservable<TResult>
The generated sequence.

The Generate operator generates a sequence of the type TState by applying the iterate function to initialState until the condition function returns false for the current state. The resultSelector function is run for each state to generate each item in the resulting sequence.

This code example uses the Generate operator to generate a sequence of the integers that are perfect squares less than 1000.

using System;
using System.Reactive.Concurrency;
using System.Reactive.Linq;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*********************************************************************************************//
      //*** Generate a sequence of integers which are the perfect squares that are less than 100. ***//
      //*********************************************************************************************//

      var obs = Observable.Generate(1,                             // Initial state value. Starting with 1.
                                    x => x * x < 1000,             // Terminate generation when false (the integer squared is not less than 1000).
                                    x => x + 1,                    // Iteration step function updates the state returning the new state. In this case state is incremented by 1. 
                                    x => x * x,                    // Selector function determines the next resulting value in the sequence. The state of type in is squared.
                                    x => TimeSpan.FromSeconds(1),  // Each item in the sequence delayed by 1 sec.
                                    Scheduler.ThreadPool);         // The ThreadPool scheduler runs the generation on a thread pool thread instead of the main thread.

      using (IDisposable handle = obs.Subscribe(x => Console.WriteLine(x)))
      {
        Console.WriteLine("Press ENTER to exit...\n");
        Console.ReadLine();
      }
    }
  }
}

The following output was generated with the code example.

Press ENTER to exit...

1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400
441
484
529
576
625
676
729
784
841
900
961
Show:
© 2014 Microsoft