PW75L2S5URX7XX5MUJKQPH36H7P4AUKQFNVOKQJKC4AFICVG2CVQC
// Package letter is for counting letters.
package letter
import (
"sync"
)
// FreqMap records the frequency of each rune in a given text.
type FreqMap map[rune]int
// Frequency counts the frequency of each rune in a given text and returns this
// data as a FreqMap.
func Frequency(s string) FreqMap {
m := FreqMap{}
for _, r := range s {
m[r]++
}
return m
}
// ConcurrentFrequency concurrently calls Frequency on each of input slice elementst,
// returns with the union of each FreqMap.
func ConcurrentFrequency(input []string) FreqMap {
freqs := make(chan FreqMap)
wg := new(sync.WaitGroup)
for _, st := range input {
wg.Add(1)
go func(st string) {
mp := Frequency(st)
freqs <- mp
}(st)
}
go func() {
wg.Wait()
close(freqs)
}()
out := FreqMap{}
for mp := range freqs {
wg.Done()
for k, v := range mp {
out[k] += v
}
}
return out
}