Observable.Generate<TState、TResult> 方法 (TState、Func<TState、Boolean>、Func<TState、TState>、Func<TState、TResult>、Func<TState、TimeSpan>、IScheduler)

通过从初始状态循环访问状态,直到条件失败,生成可观测序列。

Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (

语法

'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
    结果的类型。

parameters

  • initialState
    类型:TState
    初始状态。
  • 迭代 (iterate)
    类型: System.Func<TState、TState>
    迭代步骤函数。
  • resultSelector
    类型: System.Func<TState、TResult>
    序列中生成结果的选择器函数。
  • timeSelector
    类型: System.Func<TState、 TimeSpan>
    用于控制每次迭代生成的值速度的时间选择器函数。

返回值

类型: System.IObservable<TResult>
生成的序列。

备注

Generate 运算符通过将迭代函数应用于 initialState 来生成 TState 类型的序列,直到条件函数为当前状态返回 false。 针对每个状态运行 resultSelector 函数,以在生成的序列中生成每个项。

示例

此代码示例使用 Generate 运算符生成小于 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();
      }
    }
  }
}

以下输出是使用代码示例生成的。

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

另请参阅

参考

可观测类

生成重载

System.Reactive.Linq 命名空间