-
Notifications
You must be signed in to change notification settings - Fork 1
/
PMapIntWithWorker.go
56 lines (45 loc) · 929 Bytes
/
PMapIntWithWorker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"sync"
"github.com/logic-building/functional-go/fp"
)
func main() {
newList := PMapIntWithWorker(func(val int) int {
return val * val
}, fp.RangeInt(1, 101), 10)
fmt.Println(newList)
}
func PMapIntWithWorker(f func(int) int, list []int, worker int) []int {
if f == nil {
return []int{}
}
chJobs := make(chan int, len(list))
for _, v := range list {
chJobs <- v
}
close(chJobs)
chResult := make(chan int, worker/3)
var wg sync.WaitGroup
for i := 0; i < worker; i++ {
wg.Add(1)
go func(chResult chan int, chJobs chan int) {
defer wg.Done()
for v := range chJobs {
chResult <- f(v)
}
}(chResult, chJobs)
}
// This will wait for the workers to complete their job and then close the channel
go func() {
wg.Wait()
close(chResult)
}()
newList := make([]int, len(list))
i := 0
for v := range chResult {
newList[i] = v
i++
}
return newList
}