From ad892fce1151a515eb72bc1e23323a26c72897a3 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Sun, 24 Dec 2023 04:20:25 +0100 Subject: [PATCH] feat: add Array --- workers.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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() +} +