I have a Map<string,int> where I want to sum all values.
It will be used as word dictionary in a toy spelling corrector which is described here.
Naive attempt
let wc myMap =
Map.toSeq myMap
|> Seq.sumBy (fun kvp -> (snd kvp))
error FS0071 Consider adding further type constraints
Attempt 1
type MapFn = Map<string,int> -> int
let wc:MapFn = (function wm ->
Map.toSeq wm
|> Seq.sumBy (fun kvp -> (snd kvp)))
Attempt 2
type WordMap = WordMap of Map<string,int>
let wc (WordMap wm) =
Map.toSeq wm
|> Seq.sumBy (fun kvp -> (snd kvp))
Both attempts work, however I would like code tidier. Like sample code in Python sum(WORDS.values()).
WORDS = Counter(words(open('big.txt').read()))
def P(word, N=sum(WORDS.values())):
return WORDS[word] / N
Also wonder if type Map<string,int> is the best choice for my dictionary.