I have a c# program that reads in arbitrary CSV files. Columns can be of type int, float or double.
I am currently loading this data into a List is there any way to perform basic arithmetic operations between columns. I.e. add two columns. If the columns are of different types I would like to follow standard type promotion.
Is there an easy way to achieve this, should columns be encapsulated within an object?
Here is a code sample that shows the behaviour I am after. I do need to keep the separate types, due to memory and res
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Example
{
class Program
{
static void Main(string[] args)
{
List<IList> data = new List<IList>();
data.Add(Enumerable.Range(0, 1000).ToList()); // Add a list of ints
data.Add(Enumerable.Range(0, 1000).Select(v=>(float)v).ToList()); // Add a list of floats
data.Add(Enumerable.Range(0, 1000).Select(v => (double)v).ToList()); // Add a list of doubles
data.Add(GenerateColumn(data[0], data[1], Operation.Add));
data.Add(GenerateColumn(data[1], data[2], Operation.Divide));
}
// This is what I would do if the lists were all the same type
static IList GenerateColumn(IList colA, IList colB,Operation operation)
{
List<double> result = null;
switch (operation)
{
case Operation.Add:
result = colA.Zip(colB, (a, b) => a + b).ToList();
break;
case Operation.Subtract:
result = colA.Zip(colB, (a, b) => a - b).ToList();
break;
case Operation.Multiply:
result = colA.Zip(colB, (a, b) => a * b).ToList();
break;
case Operation.Divide:
result = colA.Zip(colB, (a, b) => a / b).ToList();
break;
}
return result;
}
public enum Operation
{
Add,
Subtract,
Multiply,
Divide
}
}
}
>) but with some of the files I need to process ran into memory limitations (already loading 15GB of data), as well as precision limits . To solve this I will predominantly be using Integers & floats.
– Hugoagogo Jan 29 '19 at 23:20