Observable.Generate<TState、TResult> メソッド (TState、Func<TState、Boolean>、Func<TState、TState>、Func<TState、TResult>、Func<TState、TimeSpan>、IScheduler)

状態が失敗するまで初期状態から状態を反復処理することによって、監視可能なシーケンスを生成します。

Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'Declaration
Public Shared Function Generate(Of TState, TResult) ( _
    initialState As TState, _
    condition As Func(Of TState, Boolean), _
    iterate As Func(Of TState, TState), _
    resultSelector As Func(Of TState, TResult), _
    timeSelector As Func(Of TState, TimeSpan), _
    scheduler As IScheduler _
) As IObservable(Of TResult)
'Usage
Dim initialState As TState
Dim condition As Func(Of TState, Boolean)
Dim iterate As Func(Of TState, TState)
Dim resultSelector As Func(Of TState, TResult)
Dim timeSelector As Func(Of TState, TimeSpan)
Dim scheduler As IScheduler
Dim returnValue As IObservable(Of TResult)

returnValue = Observable.Generate(initialState, _
    condition, iterate, resultSelector, _
    timeSelector, scheduler)
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
)
public:
generic<typename TState, typename TResult>
static IObservable<TResult>^ Generate(
    TState initialState, 
    Func<TState, bool>^ condition, 
    Func<TState, TState>^ iterate, 
    Func<TState, TResult>^ resultSelector, 
    Func<TState, TimeSpan>^ timeSelector, 
    IScheduler^ scheduler
)
static member Generate : 
        initialState:'TState * 
        condition:Func<'TState, bool> * 
        iterate:Func<'TState, 'TState> * 
        resultSelector:Func<'TState, 'TResult> * 
        timeSelector:Func<'TState, TimeSpan> * 
        scheduler:IScheduler -> IObservable<'TResult> 
JScript does not support generic types and methods.

型パラメーター

  • TState
    状態の種類。
  • TResult
    結果の型。

パラメーター

  • initialState
    型: TState
    初期状態。
  • 繰り返し (iterate)
    型: System.Func<TState、TState>
    繰り返しステップ関数。
  • resultSelector
    型: System.Func<TState、TResult>
    シーケンスで生成された結果のセレクター関数。
  • timeSelector
    種類: System.Func<TState、 TimeSpan>
    各イテレーションで生成される値の速度を制御する時間セレクター関数。

戻り値

型: System.IObservable<TResult>
生成されたシーケンス。

解説

Generate 演算子は、条件関数が現在の状態に対して false を返すまで、反復処理関数を initialState に適用することで、TState 型のシーケンスを生成します。 resultSelector 関数は、結果のシーケンス内の各項目を生成するために、状態ごとに実行されます。

このコード例では、Generate 演算子を使用して、1000 未満の完全な 2 乗である整数のシーケンスを生成します。

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();
      }
    }
  }
}

コード例で次の出力が生成されました。

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

参照

リファレンス

Observable クラス

オーバーロードの生成

System.Reactive.Linq 名前空間