feat: add Array

This commit is contained in:
ptrcnull 2023-12-24 04:20:25 +01:00
parent 7e1f27d3e5
commit ad892fce11

View file

@ -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()
}