2010-11-15 69 views
4

你能幫助我瞭解更多的細節,需要對Enumerable.Aggregate功能

 words.Aggregate((workingSentence, next) => + next + " " + workingSentence); 
從下面的代碼片段

?如果有人解釋我在C#1.1中實現這一點,那將會很棒。

(摘錄自MS) -

 string sentence = "the quick brown fox jumps over the lazy dog"; 
     // Split the string into individual words. 
     string[] words = sentence.Split(' '); 
     // Prepend each word to the beginning of the 
     // new sentence to reverse the word order. 
     string reversed = words.Aggregate((workingSentence, next) => 
               next + " " + workingSentence); 
     Console.WriteLine(reversed); 
     // This code produces the following output: 
     // 
     // dog lazy the over jumps fox brown quick the 

回答

6

你的榜樣的Aggregate部分翻譯的東西大致是這樣的:

string workingSentence = null; 
bool firstElement = true; 
foreach (string next in words) 
{ 
    if (firstElement) 
    { 
     workingSentence = next; 
     firstElement = false; 
    } 
    else 
    { 
     workingSentence = next + " " + workingSentence; 
    } 
} 
string reversed = workingSentence; 

workingSentence變量是一個累加器是每個更新通過對現有累加器值和序列的當前元素應用函數來迭代循環;這由您的示例中的lambda執行,在我的示例中由foreach循環的主體執行。

+0

感謝和清楚,我會嘗試更多的例子基於此。還要感謝Gabe和Will。 – paragy 2010-11-15 13:51:14

0

它很簡單。

string accumulatedText = string.Empty; 
foreach(string part in sentence.Split(' ')) 
    accumulatedText = part + " " + accumulatedText; 

LINQ的擴展方法是大致相同的:

// this method is the lambda 
// (workingSentence, next) => next + " " + workingSentence) 
public string Accumulate(string part, string previousResult) 
{ 
    return part + " " + previousResult; 
} 

public void Reverse(string original) 
{ 
    string retval = string.Empty; 
    foreach(var part in original.Split(' ')) 
    retval = Accumulate(part, retval); 
    return retval; 
} 
2

雖然LukeH's answer更容易理解,我認爲這是Aggregate函數調用的C#1.0翻譯的更接近於。

(workingSentence, next) => + next + " " + workingSentence是一個lambda,意思是未命名的委託。爲了翻譯它,我們必須創建一個描述它的委託類型(我稱之爲StringAggregateDelegate),然後創建函數本身(我稱之爲AggregateDelegate)。函數Aggregate自己獲取其源的第一個元素,然後遍歷剩餘的元素並使用累加結果和下一個元素調用委託。

delegate string StringAggregateDelegate(string, string); 

static string AggregateDelegate(string workingSentence, string next) 
{ 
    return next + " " + workingSentence; 
} 

static string Aggregate(IEnumerable source, 
         StringAggregateDeletate AggregateDelegate) 
{ 
    // start enumerating the source; 
    IEnumerator e = source.GetEnumerator(); 
    // return empty string if the source is empty 
    if (!e.MoveNext()) 
     return ""; 
    // get first element as our base case 
    string workingSentence = (string)e.Current; 
    // call delegate on each item after the first one 
    while (e.MoveNext()) 
     workingSentence = AggregateDelegate(workingSentence, (string)e.Current); 
    // return the result 
    return workingSentence; 
} 

// now use the Aggregate function: 
    string[] words = sentence.Split(' '); 
    // Prepend each word to the beginning of the 
    // new sentence to reverse the word order. 
    string reversed = Aggregate(words, 
           new StringAggregateDelegate(AggregateDelegate));