지정된 시드 값을 사용하여 관찰 가능한 시퀀스에 누적기 함수를 적용합니다.
네임스페이스:System.Reactive.Linq
어셈블리: System.Reactive(System.Reactive.dll)
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>
각 요소에 대해 호출할 누적기 함수입니다.
반환 값
형식: System.IObservable<TAccumulate>
최종 누적기 값이 있는 단일 요소를 포함하는 관찰 가능한 시퀀스입니다.
사용 정보
Visual Basic 및 C#에서는 IObservable TSource> 형식의 모든 개체에서 이 메서드를 instance 메서드로 호출할 수 있습니다<. 인스턴스 메서드 구문을 사용하여 이 메서드를 호출할 경우에는 첫 번째 매개 변수를 생략합니다. 자세한 내용은 또는 를 참조하세요.
설명
집계 연산자는 원본 시퀀스에 함수를 적용하여 집계 또는 누적 값을 생성하는 데 사용됩니다. 시퀀스에 적용된 함수를 누적기 함수라고 합니다. 누적기 값과 누적기 값으로 처리되는 시퀀스의 항목이라는 두 개의 매개 변수가 필요합니다. 초기 누적기 값을 시드 값이라고 하며 집계 연산자에 제공해야 합니다. accumulator 함수는 호출할 때마다 새 누적기 값을 반환합니다. 그런 다음, 새 누적기 값을 누적기 함수에 대한 다음 호출과 함께 사용하여 시퀀스에서 항목을 처리합니다. 이러한 호출은 시퀀스가 끝날 때까지 계속됩니다.
집계 연산자는 연산자에 전달되는 시드 값과 동일한 형식의 관찰 가능한 시퀀스를 반환합니다. 최종 집계 값을 얻으려면 집계 연산자에서 반환된 관찰 가능한 시퀀스를 구독합니다. 누적기 함수가 전체 시퀀스에 적용되면 구독에 제공된 관찰자의 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...