Observable.Aggregate < TSource, TAccumulate > 方法 (IObservable < TSource > , TAccumulate, Func < TAccumulate, TSource, TAccumulate >)
將累積函數套用至具有指定種子值的可觀察序列。
Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (
Syntax
'Declaration
<ExtensionAttribute> _
Public Shared Function Aggregate(Of TSource, TAccumulate) ( _
source As IObservable(Of TSource), _
seed As TAccumulate, _
accumulator As Func(Of TAccumulate, TSource, TAccumulate) _
) As IObservable(Of TAccumulate)
'Usage
Dim source As IObservable(Of TSource)
Dim seed As TAccumulate
Dim accumulator As Func(Of TAccumulate, TSource, TAccumulate)
Dim returnValue As IObservable(Of TAccumulate)
returnValue = source.Aggregate(seed, _
accumulator)
public static IObservable<TAccumulate> Aggregate<TSource, TAccumulate>(
this IObservable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> accumulator
)
[ExtensionAttribute]
public:
generic<typename TSource, typename TAccumulate>
static IObservable<TAccumulate>^ Aggregate(
IObservable<TSource>^ source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate>^ accumulator
)
static member Aggregate :
source:IObservable<'TSource> *
seed:'TAccumulate *
accumulator:Func<'TAccumulate, 'TSource, 'TAccumulate> -> IObservable<'TAccumulate>
JScript does not support generic types and methods.
類型參數
- TSource
來源的類型。
- TAccumulate
累積的類型。
參數
- source
類型:System.IObservable< TSource>
要匯總的可觀察序列。
- seed
類型:TAccumulate
初始累積值。
- 蓄電池
類型:System.Func< TAccumulate、TSource、TAccumulate>
要在每個項目上叫用 (Invoke) 的累加函式。
傳回值
類型:System.IObservable< TAccumulate>
可觀察的序列,包含具有最終累積值的單一元素。
使用注意事項
在 Visual Basic 和 C# 中,您可以在IObservable< TSource > 類型的任何物件上呼叫這個方法作為實例方法。 使用執行個體方法語法呼叫這個方法時,請省略第一個參數。 如需詳細資訊,請參閱 或 。
備註
匯總運算子可用來將函式套用至來源序列,以產生匯總或累積值。 跨序列套用的函式稱為累積函式。 它需要兩個參數:累加器值,以及序列中的專案,該序列會以累積值處理。 初始累積值稱為種子值,必須提供給匯總運算子。 每次呼叫累加器函式時,累加器函式都會傳回新的累積值。 然後,新的累積器值會與累加函式的下一次呼叫搭配使用,以處理序列中的專案。 這些呼叫會繼續直到序列結尾為止。
匯總運算子會傳回可觀察的序列,其類型與傳遞至運算子的種子值相同。 若要取得最終匯總值,您可以訂閱從匯總運算子傳回的可觀察序列。 一旦累積函數套用到整個序列,就會呼叫在訂用帳戶中提供的觀察者 OnNext 和 OnCompleted 處理常式,以提供最終匯總值。 請參閱此運算子所提供的範例程式碼。
範例
此範例示範如何使用匯總運算子,以 Console.Readkey () 在執行時間產生的字元字串中計算體音。 CountVowels 函式是累積函式,它會遞增序列中遇到的每個 Vowel 計數。
using System;
using System.Reactive.Linq;
namespace Example
{
class Program
{
enum Vowels : int
{
A, E, I, O, U
};
static void Main()
{
//****************************************************************************************//
//*** Create an observable sequence of char from console input until enter is pressed. ***//
//****************************************************************************************//
IObservable<char> xs = Observable.Create<char>(observer =>
{
bool bContinue = true;
while (bContinue)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key != ConsoleKey.Enter)
{
Console.Write(keyInfo.KeyChar);
observer.OnNext(keyInfo.KeyChar);
}
else
{
observer.OnCompleted();
Console.WriteLine("\n");
bContinue = false;
}
}
return (() => { });
});
//***************************************************************************************//
//*** ***//
//*** The "Aggregate" operator causes the accumulator function, "CountVowels", to be ***//
//*** called for each character in the sequence. ***//
//*** ***//
//*** The seed value is the integer array which will hold a count of each of the five ***//
//*** vowels encountered. It is passed as a parameter to Aggregate. ***//
//*** The seed value will be passed to CountVowels and processed with the first item ***//
//*** in the sequence. ***//
//*** ***//
//*** The return value from "CountVowels" is the same type as the seed parameter. ***//
//*** That return value is subsequently passed into each call to the accumulator with ***//
//*** its corresponding character from the sequence. ***//
// ***//
//*** The event handler, "OnNext", is not called until the accumulator function has ***//
//*** been executed across the entire sequence. ***//
//*** ***//
//***************************************************************************************//
Console.WriteLine("\nEnter a sequence of characters followed by the ENTER key.\n" +
"The example code will count the vowels you enter\n");
using (IDisposable handle = xs.Aggregate(new int[5], CountVowels).Subscribe(OnNext))
{
Console.WriteLine("\nPress ENTER to exit...");
Console.ReadLine();
}
}
//*********************************************************************************************************//
//*** ***//
//*** The Event handler, "OnNext" is called when the event stream that Aggregate is processing ***//
//** completes. ***//
//*** ***//
//*** The final accumulator value is passed to the handler. In this example, it is the array containing ***//
//*** final count of each vowel encountered. ***//
//*** ***//
//*********************************************************************************************************//
static void OnNext(int[] state)
{
Console.WriteLine("Vowel Final Count = A:{0}, E:{1}, I:{2}, O:{3}, U:{4}\n",
state[(int)Vowels.A],
state[(int)Vowels.E],
state[(int)Vowels.I],
state[(int)Vowels.O],
state[(int)Vowels.U]);
}
//*********************************************************************************************************//
//*** ***//
//*** CountVowels will be called for each character event in the event stream. ***//
//*** ***//
//*** The int array, "state", is used as the accumulator. It holds a count for each vowel. ***//
//*** ***//
//*** CountVowels simply looks at the character "ch" to see if it is a vowel and increments that vowel ***//
//*** count in the array. ***//
//*** ***//
//*********************************************************************************************************//
static int[] CountVowels(int[] state, char ch)
{
char lch = char.ToLower(ch);
switch (lch)
{
case 'a': state[(int)Vowels.A]++;
break;
case 'e': state[(int)Vowels.E]++;
break;
case 'i': state[(int)Vowels.I]++;
break;
case 'o': state[(int)Vowels.O]++;
break;
case 'u': state[(int)Vowels.U]++;
break;
};
return state;
}
}
}
以下是範例程式碼的範例輸出。
Enter a sequence of characters followed by the ENTER key.
The example code will count the vowels you enter
This is a sequence of char I am generating from Console.ReadKey()
Vowel Final Count = A:5, E:8, I:4, O:4, U:1
Press ENTER to exit...