次の方法で共有


Observable.Aggregate<TSource、TAccumulate> メソッド (IObservable<TSource>、TAccumulate、Func<TAccumulate、TSource、TAccumulate>)

指定されたシード値を持つ監視可能なシーケンスにアキュムレータ関数を適用します。

Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'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> 型の任意のオブジェクトでインスタンス メソッドとしてこのメソッドを呼び出すことができます。 インスタンス メソッド構文を使用してこのメソッドを呼び出す場合は、最初のパラメーターを省略します。 詳細については、」または」を参照してください。

解説

集計演算子は、集計値または累積値を生成するために、ソース シーケンス全体に関数を適用するために使用されます。 シーケンス全体に適用される関数は、アキュムレータ関数と呼ばれます。 アキュムレータ値とアキュムレータ値で処理されるシーケンスの項目の 2 つのパラメーターが必要です。 初期アキュムレータ値はシード値と呼ばれ、集計演算子に指定する必要があります。 アキュムレータ関数は、呼び出されるたびに新しいアキュムレータ値を返します。 その後、新しいアキュムレータ値がアキュムレータ関数の次の呼び出しと共に使用され、シーケンス内の項目が処理されます。 これらの呼び出しは、シーケンスの最後まで続行されます。

集計演算子は、演算子に渡されるシード値と同じ型の監視可能なシーケンスを返します。 最終的な集計値を取得するには、集計演算子から返された監視可能なシーケンスをサブスクライブします。 アキュムレータ関数がシーケンス全体に適用されると、サブスクリプションで提供されるオブザーバーの 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...

参照

リファレンス

Observable クラス

集計オーバーロード

System.Reactive.Linq 名前空間