• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Javen-Studio 咖啡小屋

            http://javenstudio.org - C++ Java 分布式 搜索引擎
            Naven's Research Laboratory - Thinking of Life, Imagination of Future

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              24 隨筆 :: 57 文章 :: 170 評論 :: 4 Trackbacks

            4.2         聚合操作符(Aggregation Operators

            有幾個標準查詢操作符(standard query operators)定義用來聚合(aggregating)一組值的序列(a sequence of values)成一個單獨的值(a single value)。最普通的聚合操作符(most general aggregation operator)就是 Fold,它的定義看起來如下所示:

             

            public static U Fold<T, U>(this IEnumerable<T> source, 
                                       U seed, Func
            <U, T, U> func) {
              U result 
            = seed;

              
            foreach (T element in source) 
                  result 
            = func(result, element);

              
            return result;
            }

             

            Fold 操作符使它對一組值的序列(a sequence of values)簡單地執行一個計算(perform a calculation)處理。Fold 工作的方式是對基本的序列(the underlying sequence)的每一個成員調用一次 lambda 表達式。每一次 Fold 調用 lambda 表達式的時候,它既從序列(sequence)傳遞(passes)成員(members)又傳遞一個聚合的值(aggregated value)(該初始化的值(initial value)對 Fold 是基于種子參數(seed parameter))。Lambda 表達式的結果替換了以前的聚合的值(the previous aggregated value),Fold 返回了 lambda 表達式的最終結果(the final result)。

             

            例于,下面的程序使用 Fold 累計(accumulate)計算一個字符串數組的所有字符數目(total character count):

             

            string[] names = "Albert""Burke""Connor""David",
                               
            "Everett""Frank""George""Harris"}
            ;

            int count = names.Fold(0, (c, s) => c + s.Length);
            // count == 46

             

            除了(In addition to)這個有多種用途(general purpose)的 Fold 操作符之外,標準查詢操作符(standard query operators)還包含一個多用途的 Count 操作符,和四個數字聚合(numeric aggregation)操作符(Min, Max, Sum, Average),以簡單化(simplify)這些普通的聚合操作(common aggregation operations)。數字聚合操作符檢查(work over)數字類型(numeric types)(如 int, double, decimal)的序列,或者在一個方法(function)提供的時候檢查任意值(arbitrary values)的序列(sequences),以映射(projects)序列的成員(members of the sequence)為一個數字類型(a numeric type)。

             

            下面的程序闡明(illustrates)了剛才描述的 Sum 操作符的兩個方面(both forms of):

             

            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            string[] names = { "Albert", "Burke", "Connor", "David",

                               "Everett", "Frank", "George", "Harris"};

             

            int total1 = numbers.Sum();            // total1 == 55

            int total2 = names.Sum(s => s.Length); // total2 == 46

             

            注意第二個使用 Sum 的語句與前面使用 Fold 的程序是相等的(equivalent to)。

             

             

              

             

            待續, 錯誤難免,請批評指正,譯者Naven 2005-10-24

            posted on 2005-10-25 02:00 Javen-Studio 閱讀(1262) 評論(0)  編輯 收藏 引用
            国产精品成人精品久久久| 色综合久久综合中文综合网| 97久久精品人人做人人爽| 久久久久亚洲精品男人的天堂| 中文精品99久久国产| 久久精品天天中文字幕人妻| 久久国产香蕉一区精品| 久久精品国产亚洲αv忘忧草 | 麻豆精品久久精品色综合| 色综合久久中文综合网| 四虎国产精品成人免费久久 | 大美女久久久久久j久久| 亚洲午夜无码久久久久小说| 国产成人久久激情91| 亚洲欧洲久久av| 国产精品女同一区二区久久| 亚洲精品美女久久久久99| 欧美性猛交xxxx免费看久久久| 久久久久亚洲AV无码专区体验| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 国产精品久久精品| 国产色综合久久无码有码| 久久综合久久性久99毛片| 久久香蕉一级毛片| 东京热TOKYO综合久久精品| 99久久无色码中文字幕人妻| 一本色综合久久| 久久久久久久亚洲精品| 久久九色综合九色99伊人| 国产福利电影一区二区三区,免费久久久久久久精 | 囯产极品美女高潮无套久久久 | 无码国内精品久久人妻| 国产精品99久久久精品无码 | 久久综合色之久久综合| 国产精品内射久久久久欢欢| 亚洲国产天堂久久综合网站| 久久se精品一区二区| 久久青草国产精品一区| 91久久成人免费| 久久国产乱子伦精品免费午夜| 国内精品久久久久久不卡影院|