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

31 lines
487 B
Go
Raw Normal View History

2016-08-13 23:20:14 +00:00
package letter
import (
"sync"
"time"
)
// MyConcurrentFrequency ...
func MyConcurrentFrequency(input []string) FreqMap {
var ProcMap = struct {
sync.RWMutex
m map[rune]int
}{m: make(map[rune]int)}
var procCnt = len(input)
for _, chunk := range input {
go func(chunk string) {
for _, rn := range chunk {
ProcMap.Lock()
ProcMap.m[rn]++
ProcMap.Unlock()
}
procCnt--
}(chunk)
}
for procCnt > 0 {
time.Sleep(time.Millisecond)
}
return ProcMap.m
}