Skip to content

Instantly share code, notes, and snippets.

@jackmott
Created February 23, 2017 03:56
Show Gist options
  • Select an option

  • Save jackmott/988b4742394cf5e7d331e31c0ee778d6 to your computer and use it in GitHub Desktop.

Select an option

Save jackmott/988b4742394cf5e7d331e31c0ee778d6 to your computer and use it in GitHub Desktop.
iterate over a file in F#
open System.IO
[<EntryPoint>]
let main argv =
//use gaurantees that the file handle is disposed when it goes out of scope
use s = new StreamReader ("c:\path\to\text.txt")
while not s.EndOfStream do
let s = s.ReadLine()
printfn "%s" s
//or dump it into an array
let arrayOfLines =
[|
use s = new StreamReader ("c:\path\to\text.txt")
while not s.EndOfStream do
yield s.ReadLine()
|]
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment