Created
June 25, 2020 07:41
-
-
Save ZimmerD/04da30feaf46867bd47aace1857c8bfd to your computer and use it in GitHub Desktop.
FSharpAux.IO.SeqIO.Seq.CSV: Incorrect collection formatting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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