2010-11-15 46 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));