mike alkens
mike alkens

Reputation: 53

Aggregate 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?

Upvotes: 2

Views: 7392

Answers (8)

Mc_Topaz
Mc_Topaz

Reputation: 637

Here is an example with integers:

var ints = new List<int> { 1, 2, 3 };
var sum = ints.Aggregate((c, n) => c + n);  // 1 + 2 + 3 = 6
Console.WriteLine(sum);                     // 6

Upvotes: 0

Tony Alexander Hild
Tony Alexander Hild

Reputation: 370

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

from http://msdn.microsoft.com/en-us/library/bb548651.aspx

Upvotes: 0

astorcas
astorcas

Reputation: 281

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)

Upvotes: 0

Enigmativity
Enigmativity

Reputation: 117064

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);.

Upvotes: 0

bdukes
bdukes

Reputation: 155925

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.

Upvotes: 3

Oded
Oded

Reputation: 499002

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.

Upvotes: 0

fbstj
fbstj

Reputation: 1714

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.

Upvotes: 5

Mike M.
Mike M.

Reputation: 12511

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

Foreach(y) X = X + Y;

Upvotes: 0

Related Questions