Skip to content

Instantly share code, notes, and snippets.

@SoulFireMage
Last active January 1, 2016 13:09
Show Gist options
  • Save SoulFireMage/8149590 to your computer and use it in GitHub Desktop.
Save SoulFireMage/8149590 to your computer and use it in GitHub Desktop.
Count chars in all the CSV files in a folder hierarchy without leaking memory. This gist has some meaning to me at time of writing!
let counter(str:string,ch:char) = str.ToCharArray()
|> Array.sumBy(fun s ->
let x = 0
match s with
| c when ch = s-> x + 1
| _ -> x)
let chrs = ['e';'a';'o';'s';'t']
let sample = "tesqatetweafwefwlvuioneuiounrv;onr;rvoian;c;oi;oijewrrfaa"
let character = chrs |> List.map(fun x-> counter (sample ,x))
|> List.zip chrs
|> List.map (fun (a, b)-> string a,b)
let frequencyChart = character |> Chart.Bar
let form = new Form(Visible = true ,
TopMost = true,
Width = 700,
Height = 500)
form.Controls.Add( new ChartTypes.ChartControl(frequencyChart,
Dock = DockStyle.Fill))
Application.Run(form)
let filePath = new System.IO.DirectoryInfo(@"c:\dropbox")
let countWords(x: string) = x.Split(',').Length
type fileStream() =
member this.Using( x : FileInfo, c: string -> int)=
use streamReader = x.OpenText().ReadToEndAsync()
streamReader.Result |> c
member this.Return(x : string ) = countWords x
let str = fileStream()
let WordCount = filePath.EnumerateFiles("*.csv", SearchOption.AllDirectories)
|> Seq.sumBy(fun x -> str.Using(x, countWords) )
printfn "%i" WordCount
#Load "System.Windows.Forms.DataVisualization.dll"
#Load "C:/Dropbox/fsharpstuff/FSharp.Charting.dll"
#Load "System.Windows.Forms"
#Load "System.Drawing"
open FSharp.Charting
open System.Windows.Forms
open System.Drawing
open System.IO
let filePath = new System.IO.DirectoryInfo(@"e:\Dropbox")
let wordCount (x:string) = x.Split(',').Length
type streamRead() =
member this.Using(file:FileInfo, c:string -> int) =
use x = file.OpenText()
x.ReadToEnd() |> c
let stream = streamRead()
let FileTotal = filePath.EnumerateFiles("*.csv", SearchOption.AllDirectories)
|> Seq.map (fun x->stream.Using(x, wordCount) )
|> Chart.Line
open System.IO
let findfiles = DirectoryInfo("c:\dropbox")
let countWords (str :string) = str.Split(',').Length
let count = findfiles.EnumerateFiles("*.csv", SearchOption.AllDirectories)
|> Seq.sumBy(fun f -> use a = f.OpenText()
countWords(a.ReadToEnd()))
printfn "%A" count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment