exercism/go/parallel-letter-frequency/by_the_letter.go

28 lines
584 B
Go
Raw Normal View History

2016-08-13 23:20:14 +00:00
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
}