Observable.Generate<TState, TResult> Metodu (TState, Func<TState, Boolean>, Func<TState, TState>, Func<TState, TResult>, IScheduler)
Koşul başarısız olana kadar ilk durumdan bir durumu yineleyerek gözlemlenebilir bir dizi oluşturur.
Ad Alanı:System.Reactive.Linq
Derleme: System.Reactive (System.Reactive.dll içinde)
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), _
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 scheduler As IScheduler
Dim returnValue As IObservable(Of TResult)
returnValue = Observable.Generate(initialState, _
condition, iterate, resultSelector, _
scheduler)
public static IObservable<TResult> Generate<TState, TResult>(
TState initialState,
Func<TState, bool> condition,
Func<TState, TState> iterate,
Func<TState, TResult> resultSelector,
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,
IScheduler^ scheduler
)
static member Generate :
initialState:'TState *
condition:Func<'TState, bool> *
iterate:Func<'TState, 'TState> *
resultSelector:Func<'TState, 'TResult> *
scheduler:IScheduler -> IObservable<'TResult>
JScript does not support generic types and methods.
Tür Parametreleri
- TState
Durum türü.
- Tresult
Sonucun türü.
Parametreler
- initialState
Tür: TState
İlk durum.
- Durum
Tür: System.Func<TState, Boole>
Oluşturma işlemini sonlandırma koşulu.
- Yineleme
Tür: System.Func<TState, TState>
Yineleme adımı işlevi.
- Resultselector
Tür: System.Func<TState, TResult>
Dizide üretilen sonuçlar için seçici işlevi.
- scheduler
Tür: System.Reactive.Concurrency.IScheduler
Oluşturucu döngüsünün çalıştırıldığı zamanlayıcı.
Dönüş Değeri
Tür: System.IObservable<TResult>
Oluşturulan sıra.
Açıklamalar
Generate işleci, condition işlevi geçerli durum için false döndürene kadar yineleme işlevini initialState'e uygulayarak TState türünde bir dizi oluşturur. ResultSelector işlevi, sonuçta elde edilen dizideki her öğeyi oluşturmak üzere her durum için çalıştırılır.
Örnekler
Bu kod örneği, 1000'den küçük mükemmel kareler olan tamsayıların bir dizisini oluşturmak için Generate işlecini kullanır.
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
x => x * x < 1000, // The termination condition. Terminate generation when false (the integer squared is not less than 1000).
x => x + 1, // Iteration step function updates the state and returns 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.
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();
}
}
}
}
Aşağıdaki çıkışta örnek kodun çalıştırılması gösterilmektedir.
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