Skip to content

Instantly share code, notes, and snippets.

@urasandesu
Created December 21, 2017 12:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urasandesu/dedad20e3a3d3b5c631244fcdca085af to your computer and use it in GitHub Desktop.
Save urasandesu/dedad20e3a3d3b5c631244fcdca085af to your computer and use it in GitHub Desktop.
Using F# Data as a runtime type inferencer for a csv file
using FSharp.Data;
using FSharp.Data.Runtime;
using FSharp.Data.Runtime.StructuralTypes;
using Microsoft.FSharp.Collections;
using System;
using System.Globalization;
using System.Linq;
class Program
{
static void Main(string[] args)
{
// MSFT.csv is same csv as the example of F# Data. See also http://fsharp.github.io/FSharp.Data/library/CsvProvider.html
using (var csvFile = CsvFile.Load("C:/Users/urasa/Downloads/MSFT.csv"))
{
var meth = typeof(CsvInference).GetMethods().First(_ => _.Name == "CsvFile.InferColumnTypes");
var props = (FSharpList<PrimitiveInferedProperty>)meth.Invoke(null, new object[] { csvFile, 1000, new string[0], CultureInfo.CurrentCulture, string.Empty, true, true, null });
foreach (var prop in props)
Console.WriteLine(prop);
}
}
}
// Results ------------------------
// {Name = "Date";
// InferedType = System.DateTime;
// RuntimeType = System.DateTime;
// UnitOfMeasure = null;
// TypeWrapper = Option;}
// {Name = "Open";
// InferedType = System.Decimal;
// RuntimeType = System.Decimal;
// UnitOfMeasure = null;
// TypeWrapper = Option;}
// {Name = "High";
// InferedType = System.Decimal;
// RuntimeType = System.Decimal;
// UnitOfMeasure = null;
// TypeWrapper = Option;}
// {Name = "Low";
// InferedType = System.Decimal;
// RuntimeType = System.Decimal;
// UnitOfMeasure = null;
// TypeWrapper = Option;}
// {Name = "Close";
// InferedType = System.Decimal;
// RuntimeType = System.Decimal;
// UnitOfMeasure = null;
// TypeWrapper = Option;}
// {Name = "Volume";
// InferedType = System.Int32;
// RuntimeType = System.Int32;
// UnitOfMeasure = null;
// TypeWrapper = Option;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment