Created
June 13, 2013 08:54
-
-
Save wilhelmjung/5772254 to your computer and use it in GitHub Desktop.
Words frequency statistic by async syntax in F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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