31 people like it.

Dumping record type sequence to file

Easy dump of record type sequence to file

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
open Microsoft.FSharp.Reflection

let toSepFile sep header (fileName:string) (s:'record seq)=
 
    let schemaType=typeof<'record>
    let fields = Reflection.FSharpType.GetRecordFields(schemaType) 

    let toStr fields = 
        fields
        |> Seq.fold(fun res field-> res+field+sep) ""
    
    use w = new System.IO.StreamWriter(fileName)

    if header then 
        let header_str= fields
                        |> Seq.map(fun field -> field.Name)
                        |> toStr
                    
        w.WriteLine(header_str)

    let elemToStr (elem:'record) = 
        //for each field get value 
        fields
        |> Seq.map(fun field -> string (FSharpValue.GetRecordField(elem,field)))
        |> toStr
   
    s
    |>Seq.map(elemToStr)
    |>Seq.iter(fun elem -> w.WriteLine(elem))
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Reflection
val toSepFile : sep:string -> header:bool -> fileName:string -> s:seq<'record> -> unit

Full name: Script.toSepFile
val sep : string
val header : bool
val fileName : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val s : seq<'record>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val schemaType : System.Type
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
val fields : System.Reflection.PropertyInfo []
type FSharpType =
  static member GetExceptionFields : exceptionType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
  static member GetFunctionElements : functionType:Type -> Type * Type
  static member GetRecordFields : recordType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
  static member GetTupleElements : tupleType:Type -> Type []
  static member GetUnionCases : unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo []
  static member IsExceptionRepresentation : exceptionType:Type * ?bindingFlags:BindingFlags -> bool
  static member IsFunction : typ:Type -> bool
  static member IsModule : typ:Type -> bool
  static member IsRecord : typ:Type * ?bindingFlags:BindingFlags -> bool
  static member IsTuple : typ:Type -> bool
  ...

Full name: Microsoft.FSharp.Reflection.FSharpType
static member FSharpType.GetRecordFields : recordType:System.Type * ?allowAccessToPrivateRepresentation:bool -> System.Reflection.PropertyInfo []
static member FSharpType.GetRecordFields : recordType:System.Type * ?bindingFlags:System.Reflection.BindingFlags -> System.Reflection.PropertyInfo []
val toStr : (seq<string> -> string)
val fields : seq<string>
module Seq

from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val res : string
val field : string
val w : System.IO.StreamWriter
namespace System
namespace System.IO
Multiple items
type StreamWriter =
  inherit TextWriter
  new : stream:Stream -> StreamWriter + 6 overloads
  member AutoFlush : bool with get, set
  member BaseStream : Stream
  member Close : unit -> unit
  member Encoding : Encoding
  member Flush : unit -> unit
  member Write : value:char -> unit + 3 overloads
  static val Null : StreamWriter

Full name: System.IO.StreamWriter

--------------------
System.IO.StreamWriter(stream: System.IO.Stream) : unit
System.IO.StreamWriter(path: string) : unit
System.IO.StreamWriter(stream: System.IO.Stream, encoding: System.Text.Encoding) : unit
System.IO.StreamWriter(path: string, append: bool) : unit
System.IO.StreamWriter(stream: System.IO.Stream, encoding: System.Text.Encoding, bufferSize: int) : unit
System.IO.StreamWriter(path: string, append: bool, encoding: System.Text.Encoding) : unit
System.IO.StreamWriter(path: string, append: bool, encoding: System.Text.Encoding, bufferSize: int) : unit
val header_str : string
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val field : System.Reflection.PropertyInfo
property System.Reflection.MemberInfo.Name: string
System.IO.TextWriter.WriteLine() : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: obj) : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: string) : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: decimal) : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: float) : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: float32) : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: uint64) : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: int64) : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: uint32) : unit
   (+0 other overloads)
System.IO.TextWriter.WriteLine(value: int) : unit
   (+0 other overloads)
val elemToStr : ('record -> string)
val elem : 'record
type FSharpValue =
  static member GetExceptionFields : exn:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetRecordField : record:obj * info:PropertyInfo -> obj
  static member GetRecordFields : record:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetTupleField : tuple:obj * index:int -> obj
  static member GetTupleFields : tuple:obj -> obj []
  static member GetUnionFields : value:obj * unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo * obj []
  static member MakeFunction : functionType:Type * implementation:(obj -> obj) -> obj
  static member MakeRecord : recordType:Type * values:obj [] * ?bindingFlags:BindingFlags -> obj
  static member MakeTuple : tupleElements:obj [] * tupleType:Type -> obj
  static member MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:BindingFlags -> obj
  ...

Full name: Microsoft.FSharp.Reflection.FSharpValue
static member FSharpValue.GetRecordField : record:obj * info:System.Reflection.PropertyInfo -> obj
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val elem : string

More information

Link:http://fssnip.net/3y
Posted:13 years ago
Author:Chief Inspector Clouseau
Tags: data , file writing