More information on the Enumerable.Aggregate function is required.

Can you help me understand

words.Aggregate((workingSentence, next) => + next + " " + workingSentence); 

below code snippet? and it would be great if someone explained to me how to do this in C # 1.1.

(Snapshot from 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 
+4
source share
3 answers

The Aggregate part of your example translates something like this:

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

The workingSentence variable is a battery that is updated at each iteration of the cycle, applying the function to the existing battery value and the current element of the sequence; this is done by the lambda in your example and the body of the foreach in my example.

+6
source

Although LukeH's answer is easier to understand, I think this is a closer approximation of translating a C # 1.0 function call to an Aggregate function.

(workingSentence, next) => + next + " " + workingSentence is a lambda, which means an unnamed delegate. To translate it, we need to create a delegate type that describes it (I call it StringAggregateDelegate ), and then make this function StringAggregateDelegate (I call it AggregateDelegate ). The Aggregate function itself receives the first element of its source, then iterates over the remaining elements and calls the delegate with the accumulated result and the next element.

 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)); 
+2
source

Its pretty simple.

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

The linq extension method is roughly equivalent to this:

 // 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; } 
0
source

All Articles