feat: add Array
This commit is contained in:
parent
7e1f27d3e5
commit
ad892fce11
1 changed files with 16 additions and 0 deletions
16
workers.go
16
workers.go
|
@ -16,3 +16,19 @@ func Channel[T any](n int, ch chan T, handler func(T)) {
|
|||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// Array launches n workers to perform an action for every item in the input array
|
||||
func Array[T any](n int, arr []T, handler func(T)) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(n)
|
||||
for i := 0; i < n; i++ {
|
||||
go func(i int) {
|
||||
for j := 0; (j*n)+i < len(arr); j++ {
|
||||
handler(arr[(j*n)+i])
|
||||
}
|
||||
wg.Done()
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue