Skip to content

Instantly share code, notes, and snippets.

@wilhelmjung
Created June 13, 2013 08:54
Show Gist options
  • Select an option

  • Save wilhelmjung/5772254 to your computer and use it in GitHub Desktop.

Select an option

Save wilhelmjung/5772254 to your computer and use it in GitHub Desktop.
Words frequency statistic by async syntax in F#
open System
open System.IO
open System.Text.RegularExpressions
open Microsoft.FSharp.Control.CommonExtensions
let path = @"C:\Users\William\Desktop\log\txt"
let readFileAsync filePath =
async { // open and read file
let fileStream = File.OpenText(filePath)
let! text = fileStream.ReadToEndAsync() |> Async.AwaitTask
// find all the "words" using a regex
let word = new Regex("\w+")
let matches = word.Matches(text)
let words = seq { for m in matches -> m.Value }
// count unique words using a set
let uniqueWords = Set.ofSeq words
// print the results
let name = Path.GetFileNameWithoutExtension(filePath)
do Console.WriteLine("{0} - Words: {1} Unique words: {2} ",
name, matches.Count, uniqueWords.Count)
}
let main() =
let filePaths = Directory.GetFiles(path)
let tasks = [ for filePath in filePaths -> readFileAsync filePath ]
Async.RunSynchronously (Async.Parallel tasks)
//
main() |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment