Skip to content

Commit

Permalink
Merge pull request #112 from matthewreily/additional-aggregate-samples
Browse files Browse the repository at this point in the history
Additional aggregate samples
  • Loading branch information
BethMassi committed Nov 12, 2015
2 parents 3fb85fc + 3911626 commit ea006cd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
4 changes: 2 additions & 2 deletions samples/linq/csharp/aggregate/Aggregate-Sample-1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Aggregate
{
public class AggregateSample1
public static class AggregateSample1
{
//This sample uses Aggregate to create a running product on the array that calculates the total product of all elements.
//Output:
//Total product of all numbers: 88.33081
public void MethodSyntaxExample()
public static void MethodSyntaxExample()
{
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };

Expand Down
32 changes: 16 additions & 16 deletions samples/linq/csharp/aggregate/Aggregate-Sample-2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@

namespace Aggregate
{
public class AggregateSample2
{
//This sample uses Aggregate to create a running account balance that subtracts each
public static class AggregateSample2
{
//This sample uses Aggregate to create a running account balance that subtracts each
// withdrawal from the initial balance of 100, as long as the balance never drops below 0.
//Output:
// Ending balance: 20
public void MethodSyntaxExample()
{
double startBalance = 100.0;
//Output:
// Ending balance: 20
public static void MethodSyntaxExample()
{
double startBalance = 100.0;

int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 };
int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 };

double endBalance =
attemptedWithdrawals.Aggregate(startBalance,
(balance, nextWithdrawal) =>
((nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance));
double endBalance =
attemptedWithdrawals.Aggregate(startBalance,
(balance, nextWithdrawal) =>
((nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance));

Console.WriteLine("Ending balance: {0}", endBalance);
}
}
Console.WriteLine("Ending balance: {0}", endBalance);
}
}
}
35 changes: 35 additions & 0 deletions samples/linq/csharp/aggregate/Count-Sample-1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Linq;
using System;

namespace Aggregate
{
public static class CountSample1
{
//This sample uses Count to get the number of unique factors of 300 using method syntax.
//
// Output:
// There are 3 unique factors of 300.
public static void MethodSyntaxExample()
{
int[] factorsOf300 = { 2, 2, 3, 5, 5 };

int uniqueFactors = factorsOf300.Distinct().Count();

Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
}

//This sample uses Count to get the number of unique factors of 300 using query syntax.
//
// Output:
// There are 3 unique factors of 300.
public static void QuerySyntaxExample()
{
int[] factorsOf300 = { 2, 2, 3, 5, 5 };

int uniqueFactors =
(from f in factorsOf300 select f).Distinct().Count();

Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
}
}
}
16 changes: 9 additions & 7 deletions samples/linq/csharp/aggregate/program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
public class Program
{
public void Main(string[] args)
{
public void Main(string[] args)
{
var sample1 = new Aggregate.AggregateSample1();
sample1.MethodSyntaxExample();
Aggregate.AggregateSample1.MethodSyntaxExample();

var sample2 = new Aggregate.AggregateSample2();
sample2.MethodSyntaxExample();
}
Aggregate.AggregateSample2.MethodSyntaxExample();

Aggregate.CountSample1.MethodSyntaxExample();

Aggregate.CountSample1.QuerySyntaxExample();
}
}

0 comments on commit ea006cd

Please sign in to comment.