public void AggregateMethodTest()
{
// Average(), Max(), Min(), Sum()은 helper
// Decimal, Double, Int32, Int64, Single type 지원
{
string sentence = "the quick brown fox jumps over the lazy dog";
string[] words = sentence.Split(' ');
string reversed = words.Aggregate(
(workingSentence, next) => next + " " + workingSentence);
Assert.AreEqual(
"dog lazy the over jumps fox brown quick the",
reversed);
}
{
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
const int SEED = 0;
Assert.AreEqual(
6,
ints.Aggregate(SEED, (total, next) => next % 2 == 0 ? total + 1 : total));
}
{
string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };
const string SEED = "banana";
string longestName =
fruits.Aggregate(
SEED,
(longest, next) => next.Length > longest.Length ? next : longest,
fruit => fruit.ToUpper()); // result selector
Assert.AreEqual("PASSIONFRUIT", longestName);
}
}