Using the Map type

Category:
Data Types
Description: Create a histogram of the occurrences of particular unicode characters using a Microsoft.FSharp.Collections.Map.
Code:
let SampleMap1() =
let data = "The quick brown fox jumps over the lazy dog"
let histogram =
data.ToCharArray()
|> Seq.groupBy (fun c -> c)
|> Map.ofSeq
|> Map.map (fun k v -> Seq.length v)
for (KeyValue(c,n)) in histogram do
printfn "Number of '%c' characters = %d" c n

Execution Result:
Number of ' ' characters = 8
Number of 'T' characters = 1
Number of 'a' characters = 1
Number of 'b' characters = 1
Number of 'c' characters = 1
Number of 'd' characters = 1
Number of 'e' characters = 3
Number of 'f' characters = 1
Number of 'g' characters = 1
Number of 'h' characters = 2
Number of 'i' characters = 1
Number of 'j' characters = 1
Number of 'k' characters = 1
Number of 'l' characters = 1
Number of 'm' characters = 1
Number of 'n' characters = 1
Number of 'o' characters = 4
Number of 'p' characters = 1
Number of 'q' characters = 1
Number of 'r' characters = 2
Number of 's' characters = 1
Number of 't' characters = 1
Number of 'u' characters = 2
Number of 'v' characters = 1
Number of 'w' characters = 1
Number of 'x' characters = 1
Number of 'y' characters = 1
Number of 'z' characters = 1

Last edited Sep 14, 2011 at 2:28 AM by ttliu2000, version 1

Comments

No comments yet.