87

7
+1  Q:

## Need somebody to help explain the following code (lambda expressions)

``````int sum0 = 0;
for (int i = 0; i < 10; i++)
{
sum0 += i;
}

int sum1 = Enumerable.Range(0, 10).Sum();
int sum2 = Enumerable.Range(0, 10).Aggregate((x, y) => x + y);
int sum3 = Enumerable.Range(0, 10).Aggregate(0, (x, y) => x + y);
``````

All of the above 4 expressions are doing the same thing: find sum from 0 to 10. I understand the calculation of sum0 and sum1. But what are sum2 and sum3? Why the lambda uses two parameters (x, y) here? Thanks in advance.

+2  A:

The `Enumerable.Aggregate` method expects a function that takes the current value of the aggregation and a value from the enumeration. The overload for `sum3` also provides a starting value for the aggregation.

A:

X is holding the current total, Y is being added to it for each element.

Foreach(y) X = X + Y;

So X is the returned value, Y is the variable to be add? Thanks Mike!
+4  A:

Expanding on bdukes' answer, the lambda takes

``````( x = [value of last lambda expression], y = [next value] ) => x+y
``````

and sum3 allows you to set the initial x value.

A:

The `Aggregate` extension methods take a function (`Func<T1,T2,TResult>`) that calculates an aggregate..

The function specified for `sum2` is one that adds `x` to `y`, for every supplied `x` and `y` (that is it sums all the items in the enumeration).

The additional parameter for `sum3` is an accumulator - a value that is to be added for each operation - as this is 0, it is essentially summing up all the items in the enumeration without any additional values.

A:

`sum2` uses a custom function `x + y` to aggregate each element of the list. The aggregation starts with the default value for an integer `0` and adds the first element to it. It then takes that value and adds the next element, and so on, until it runs out of elements. It then returns the final figure.

`sum3` does exactly the same as `sum2` but it also explicitly starts the aggregation with a specific value of `0`.

Semantically all three are the same - as presented here - but by varying the aggregation function and the initial starting value you can generate all sorts of custom aggregations.

Another way of looking at it is that `.Sum()` is simply short-hand for `.Aggregate(0, (x, y) => x + y);`.

A:

x is the variable that "accumulates" the values so its value is

``````step 1) 1
step 2) 3
step 3) 6
step 4) 10
``````

and so on...

the 0 in the sum3 is the starting value :) (which is redundant since 0 is the default value for int)

A:

The parameter x holds the aggregation, and y is the next enumeration item.

``````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
``````