다음을 통해 공유


Observable.Generate<TState, TResult> 메서드(TState, Func<TState, Boolean>, Func<TState, TState>, Func<TState, TResult>, Func<TState, TimeSpan>, IScheduler)

조건이 실패할 때까지 초기 상태에서 상태를 반복하여 관찰 가능한 시퀀스를 생성합니다.

네임스페이스:System.Reactive.Linq
어셈블리: System.Reactive(System.Reactive.dll)

Syntax

'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
    초기 상태입니다.
  • condition(조건)
    형식: System.Func<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보다 작은 완벽한 정수 시퀀스를 생성합니다.

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 네임스페이스