Skip to content

Instantly share code, notes, and snippets.

@MNie
Created November 6, 2017 16:30
Show Gist options
  • Save MNie/04763d141230540c8ddab1948dad3b0d to your computer and use it in GitHub Desktop.
Save MNie/04763d141230540c8ddab1948dad3b0d to your computer and use it in GitHub Desktop.
type CsvParser(path: string) =
let _path = path
let index(headers: string[] option, column) = headers.Value |> Seq.findIndex (fun k -> k = column)
 
let getValuesFor(func) =
use data = CsvFile.Load(_path)
data.Rows
|> Seq.map(fun x -> func(data.Headers, x))
|> Seq.toArray
 
member this.GetHeaders() =
use data = CsvFile.Load(_path)
if data.Headers.IsSome then data.Headers.Value else [||]
 
member this.GetValuesForColumn1() = getValuesFor(fun (headers, x) -> x.Item(index(headers, "Column 1")))
member this.GetValuesForColumn2() = getValuesFor(fun (headers, x) -> x.Item(index(headers, "Column 2")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment