Skip to content

Instantly share code, notes, and snippets.

@ZimmerD
Created June 25, 2020 07:41
Show Gist options
  • Save ZimmerD/04da30feaf46867bd47aace1857c8bfd to your computer and use it in GitHub Desktop.
Save ZimmerD/04da30feaf46867bd47aace1857c8bfd to your computer and use it in GitHub Desktop.
FSharpAux.IO.SeqIO.Seq.CSV: Incorrect collection formatting
open BioFSharp
open FSharpAux.IO.SeqIO
///Returns a function to format a given value as string
let inline stringFunction (separator: string) (flatten: bool) (input: 'a) =
let o = box input
match o with
//match string first so that it doesn't get treated as a char array
| :? string ->
fun (x: obj) ->
let sb = new System.Text.StringBuilder()
sb.Append x |> ignore
let res = sb.ToString()
sb.Clear() |> ignore
res
| :? System.Collections.IEnumerable ->
if flatten then
fun x ->
let sb = new System.Text.StringBuilder()
let a = x :?> System.Collections.IEnumerable
//iterates over Collections.IEnumerable to get entries as objects for the string builder
let b = [for i in a do yield box i]
b
|> Seq.iteri (fun i x ->
if i = 0 then
sb.AppendFormat("{0}", x) |> ignore
else
sb.AppendFormat(sprintf "%s{0}" separator, x) |> ignore
)
let res = sb.ToString()
sb.Clear() |> ignore
res
else
fun x ->
let standardSeparators = [";";"\t";","]
let separator =
standardSeparators
|> List.find (fun sep -> sep <> separator)
let sb = new System.Text.StringBuilder()
let a = x :?> System.Collections.IEnumerable
//iterates over Collections.IEnumerable to get entries as objects for the string builder
let b = [for i in a do yield box i]
b
|> Seq.iteri (fun i x ->
if i = 0 then
sb.AppendFormat("{0}", x) |> ignore
else
sb.AppendFormat(sprintf "%s{0}" separator, x) |> ignore
)
let res = sb.ToString()
sb.Clear() |> ignore
res
| _ ->
fun (x: obj) ->
let sb = new System.Text.StringBuilder()
sb.Append x |> ignore
let res = sb.ToString()
sb.Clear() |> ignore
res
let csv separator header flatten data = Seq.CSVwith Seq.valueFunction stringFunction separator header flatten data
type test = {
name : string
lname : string
d1 : string []
d2 : float list
d3 : int seq
}
let x =
[
for i = 1 to 10 do
yield
{
name = i.ToString()
lname = i.ToString()
d1 = [|i..50|] |> Array.map string
d2 = [i..50] |> List.map float
d3 = seq {i.. 50}
}
]
let outFilePath = @"YourPath"
x
|> csv "\t" true false
|> FSharpAux.IO.SeqIO.Seq.writeOrAppend (outFilePath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment