对具有指定种子值的可观测序列应用累加器函数。
Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (
语法
'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
累加的类型。
parameters
- source
类型: System.IObservable<TSource>
要聚合的可观察序列。
- seed
类型:TAccumulate
累加器的初始值。
- 蓄电池
类型: System.Func<TAccumulate、TSource、TAccumulate>
要对每个元素调用的累加器函数。
返回值
类型: System.IObservable<TAccumulate>
包含具有最终累加器值的单个元素的可观测序列。
使用说明
在 Visual Basic 和 C# 中,可以在 IObservable<TSource> 类型的任何对象上调用此方法作为实例方法。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅或。
备注
聚合运算符用于跨源序列应用函数,以生成聚合或累积值。 跨序列应用的函数称为累加器函数。 它需要两个参数:累加器值和序列中的项(使用累加器值进行处理)。 初始累加器值称为种子值,必须提供给聚合运算符。 每次调用累加器函数时,累加器函数都会返回新的累加器值。 然后,新累加器值与下一次调用累加器函数一起使用,以处理序列中的项。 这些调用一直持续到序列结束。
聚合运算符返回一个可观测序列,该序列与传递到 运算符中的种子值的类型相同。 若要获取最终聚合值,请订阅从聚合运算符返回的可观测序列。 在整个序列中应用累加器函数后,将调用订阅中提供的观察程序的 OnNext 和 OnCompleted 处理程序来提供最终的聚合值。 请参阅此运算符提供的示例代码。
示例
此示例演示如何使用聚合运算符通过 Console.Readkey () 对运行时生成的字符串中的元音进行计数。 CountVowels 函数是累加器函数,它递增序列中遇到的每个元音的计数。
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...