aboutsummaryrefslogtreecommitdiff
path: root/each.go
diff options
context:
space:
mode:
Diffstat (limited to 'each.go')
-rw-r--r--each.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/each.go b/each.go
new file mode 100644
index 0000000..d115054
--- /dev/null
+++ b/each.go
@@ -0,0 +1,23 @@
+package collection
+
+import "sync"
+
+func EachSync[T any](collection []T, cb func(item T, idx int)) {
+ for i, v := range collection {
+ cb(v, i)
+ }
+}
+
+func Each[T any](collection []T, cb func(item T, idx int)) {
+ wg := sync.WaitGroup{}
+ wg.Add(len(collection))
+ for i, v := range collection {
+ func(i int, v T) {
+ go func() {
+ defer wg.Done()
+ cb(v, i)
+ }()
+ }(i, v)
+ }
+ wg.Wait()
+}