F#: Generating a word count summary -
i new programming , f# first .net language.
i read contents of text file, count number of occurrences of each word, , return 10 common words , number of times each of them appears.
my questions are: using dictionary encouraged in f#? how write code if wish use dictionary? (i have browsed through dictionary class on msdn, still puzzling on how can update value key.) have resort using map in functional programming?
while there's nothing wrong other answers, i'd point out there's specialized function number of unique keys in sequence: seq.countby
. plumbing relevant parts of reed's , torbonde's answers together:
let countwordstopten (s : string) = s.split([|','|]) |> seq.countby (fun s -> s.trim()) |> seq.sortby (snd >> (~-)) |> seq.truncate 10 "one, two, one, three, four, one, two, four, five" |> countwordstopten |> printfn "%a" // seq [("one", 3); ("two", 2); ("four", 2); ("three", 1); ...]
Comments
Post a Comment