Skip to content

Instantly share code, notes, and snippets.

@shwars
Created March 17, 2014 06:56
Show Gist options
  • Save shwars/9595019 to your computer and use it in GitHub Desktop.
Save shwars/9595019 to your computer and use it in GitHub Desktop.
Demonstrating how to process text file using sequences in F#
open System.IO
let ReadLines fn =
seq { use inp = File.OpenText fn in
while not(inp.EndOfStream) do
yield (inp.ReadLine())
}
#load @"FSharpChart.fsx"
#load @"FSharpChartAutoDisplay.fsx"
open Samples.Charting
type System.String with
member x.GoodSplit() = x.Split([|'"';' ';'!';'.';',';';';'-'|])
let file = ReadLines @"c:\books\wap_1.txt"
file
|> Seq.map (fun x -> x.GoodSplit())
|> Seq.concat
|> Seq.filter (fun x->x.Length>3)
|> Seq.groupBy (fun x->x.ToLower())
|> Seq.map (fun (l,s) -> (l,-Seq.length s))
|> Seq.sortBy snd
|> Seq.take 5
|> FSharpChart.Bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment