private static IEnumerable <double> ExampleAveragePricesMerge() { var stockPrices = new[] { // IBM APL TIX NNTP new[] { 10.0, 11.0, 15.0, 9.0 }, // day 1 new[] { 4.0, 3.8, 4.1, 3.1 }, // day 2 new[] { 9.0, 8.0, 18.3, 22.3 } // day 3 }; // sum the columns var res = ZipEnumerable.Return(0.0); // provides an initial field to begin summing the // columns against; merging against the // rows in stockPrices will produce rows // with the same length. foreach (var day in stockPrices) { res = from pair in res.Merge(day) select pair.Item1 + pair.Item2; } // result is the average of each column in a new row var averagePrices = from sum in res select(sum / stockPrices.Count()); return(averagePrices); }
private static IEnumerable <double> ExampleAveragePricesLINQ() { var stockPrices = new[] { // IBM APL TIX NNTP new[] { 10.0, 11.0, 15.0, 9.0 }, // day 1 new[] { 4.0, 3.8, 4.1, 3.1 }, // day 2 new[] { 9.0, 8.0, 18.3, 22.3 } // day 3 }; // sum the columns var res = ZipEnumerable.Return(0.0); // provides an initial field to begin summing the // columns against; merging against the // rows in stockPrices will produce rows // with the same length. foreach (var day in stockPrices) { res = from runningSum in res join price in day on 1 equals 1 // hacking join this way is the same as Merge(), // meaning we will get runningSum at position x // coupled with price at position x in the day select runningSum + price; } // result is the average of each column in a new row var averagePrices = from sum in res select(sum / stockPrices.Count()); return(averagePrices); }