Skip to content

Instantly share code, notes, and snippets.

@michaeloyer
Last active January 9, 2022 22:47
Show Gist options
  • Save michaeloyer/6ed37e70d2b46c2c812318f1e1274cf8 to your computer and use it in GitHub Desktop.
Save michaeloyer/6ed37e70d2b46c2c812318f1e1274cf8 to your computer and use it in GitHub Desktop.
Example Showing how CSV Provider will use chunks of a stream while iterating
#r "nuget:FSharp.Data"
open System.IO
open System.Text
open FSharp.Data
type Csv = CsvProvider<"A,B,C\n1,2,3", CacheRows=false>
let csvStream =
let stream = new MemoryStream()
stream.Write(Encoding.UTF8.GetBytes("A,B,C\n"))
for i in 1..500 do
stream.Write(Encoding.UTF8.GetBytes($"{i},{i+1},{i+2}\n"))
stream.Position <- 0
stream
let mutable position: int64 = 0
for row in Csv.Load(csvStream).Rows do
if position <> csvStream.Position then
position <- csvStream.Position
printfn $"A = {row.A}; B = {row.B}; C = {row.C}; StreamPosition = {csvStream.Position}"
A = 1; B = 2; C = 3; StreamPosition = 1024
A = 112; B = 113; C = 114; StreamPosition = 2048
A = 197; B = 198; C = 199; StreamPosition = 3072
A = 283; B = 284; C = 285; StreamPosition = 4096
A = 368; B = 369; C = 370; StreamPosition = 5120
A = 453; B = 454; C = 455; StreamPosition = 5688
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment