static void GenerateSubsetClient() { Console.WriteLine("All possible subsets:"); var watch = Stopwatch.StartNew(); var resultSet = from subset in Program4.BigSubsetsP <int>(new List <int>() { 3, 1, 1, 2, 2, 1 }).AsParallel() where subset.Sum() == 5 select subset; foreach (var set in resultSet) { Console.WriteLine("SET >> {0} :: SUM >> {1}", string.Join(",", set), set.Sum()); } Console.WriteLine("Elapsed Time for Subset Generation : {0} seconds", watch.ElapsedMilliseconds / 1000D); }
static void Main(string[] args) { #region "Function Type" int startEven = -2; int startOdd = -1; int increment = 2; //To Generate first 1000 even numbers IEnumerable <int> evenSequences = GenerateSequences <int>( 1000, () => startEven += increment); //To Generate first 1000 odd numbers IEnumerable <int> oddSequences = GenerateSequences <int>( 1000, () => startOdd += increment); #endregion #region "Closures" var add10 = sum(10); //Returns closure with 10 var add50 = sum(50); //Returns closure with 50 Console.WriteLine(add10(90)); //Returns 100 Console.WriteLine(add10(190)); //Returns 200 Console.WriteLine(add50(70)); //Returns 120 #endregion //Regular Recursion Func <int, int> factorial = (n) => { Func <int, int> fIterator = null;//Work-around for "use of unassigned variable" error! fIterator = (m) => (m < 2) ? 1 : m *fIterator(m - 1); return(fIterator(n)); }; //Tail Recursion Func <int, int> factorial1 = (n) => { Func <int, int, int> fIterator = null; fIterator = (product, i) => (i < 2) ? product : fIterator(product * i, i - 1); return(fIterator(1, n)); }; //Tail Recursion with Trampolining Func <int, int> factorial3 = (n) => { Func <int, int, int> trampoline = null; Func <int, int, int> fIterator = (product, i) => (i < 2) ? product : trampoline(product * i, i - 1); trampoline = (product, i) => fIterator(product * i, i - 1); return(trampoline(1, n)); }; #region "Currying and partial application - 2" var curriedSum1 = Program4.Curry <int, int, int>(Program4.AddOperation); Console.WriteLine( "Sum: {0}", curriedSum1(10)(90) ); //Prints Sum: 100 Func <int, int, int> op = Program4.AddOperation; var curriedSum = op.Curry <int, int, int>(); Console.WriteLine("Fact: {0}", factorial(6)); //Prints Sum: 100 Console.WriteLine("Sum: {0}", curriedSum(100)(90)); //Prints Sum: 100 Func <string, string, string> op2 = Program4.ConcatOperation; var curriedConcat = op2.Curry <string, string, string>(); Console.WriteLine( "Concatenation: {0}", curriedConcat("Currying ")("Rocks!!!") ); //Prints "Concatenation: Currying Rocks!!!" Func <string, string, string, string, string> op3 = Program4.ConcatOperation; var partiallyAppliedConcat = op3. PartialApply <string, string, string, string, string> ("Partial "); Console.WriteLine( "Concatenation: {0}", partiallyAppliedConcat( "Function ", "Application ", "Rocks!!!") ); //Prints "Concatenation: Partial Function Application Rocks!!!" #endregion ContinuationClient(); SpellCheckerClient(); GenerateSubsetClient(); Console.WriteLine("PythagoreanTriples..."); var watch = Stopwatch.StartNew(); var count = (from triplets in PythagoreanTriples(50) select triplets).Count(); Console.WriteLine("No of PythagoreanTriples Within 50: {0} determined in {1} seconds", count, watch.ElapsedMilliseconds / 1000D); watch = Stopwatch.StartNew(); count = (from triplets in PythagoreanTriplesCurried(50) select triplets).Count(); Console.WriteLine("PythagoreanTriples Within 50 Using Curried Functions: {0} determined in {1} seconds", count, watch.ElapsedMilliseconds / 1000D); watch = Stopwatch.StartNew(); count = (from triplets in PythagoreanTriplesPartiallyApplied(50) select triplets).Count(); Console.WriteLine("PythagoreanTriples Within 50 Using Partially Applied Functions: {0} determined in {1} seconds", count, watch.ElapsedMilliseconds / 1000D); Console.WriteLine("PythagoreanTriples within 50...."); foreach (var triplets in PythagoreanTriplesPartiallyApplied(50)) { Console.WriteLine(string.Join(",", triplets.ToArray())); } GenerateBoxingCombinations(); Console.ReadLine(); }