diff --git a/workers.go b/workers.go index 923f04a..f472f89 100644 --- a/workers.go +++ b/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() +} +