28 lines
584 B
Go
28 lines
584 B
Go
|
package letter
|
||
|
|
||
|
import "time"
|
||
|
|
||
|
// ConcurrentFrequency concurrently calls Frequency
|
||
|
// and returns the final Frequency Map
|
||
|
func ConcurrentFrequency(input []string) FreqMap {
|
||
|
var procCnt = len(input)
|
||
|
var toMerge []FreqMap
|
||
|
for _, chunk := range input {
|
||
|
go func(chunk string) {
|
||
|
toMerge = append(toMerge, Frequency(chunk))
|
||
|
procCnt--
|
||
|
}(chunk)
|
||
|
}
|
||
|
for procCnt > 0 {
|
||
|
time.Sleep(time.Millisecond)
|
||
|
}
|
||
|
// We should have all FreqMaps now... merge them
|
||
|
ret := make(map[rune]int)
|
||
|
for _, aResult := range toMerge {
|
||
|
for k, v := range aResult {
|
||
|
ret[k] += v
|
||
|
}
|
||
|
}
|
||
|
return ret
|
||
|
}
|