From 9cd6b0fca87679e570fbdd4942e3068feb3439fa Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Thu, 7 Apr 2022 21:36:40 +0300 Subject: initial --- map.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 map.go (limited to 'map.go') diff --git a/map.go b/map.go new file mode 100644 index 0000000..065a6a6 --- /dev/null +++ b/map.go @@ -0,0 +1,27 @@ +package collection + +import "sync" + +func MapSync[T any, R any](collection []T, cb func(item T, idx int) R) []R { + result := make([]R, len(collection)) + for i, v := range collection { + result[i] = cb(v, i) + } + return result +} + +func Map[T any, R any](collection []T, cb func(item T, idx int) R) []R { + result := make([]R, len(collection)) + wg := sync.WaitGroup{} + wg.Add(len(collection)) + for i, v := range collection { + func(v T, i int) { + go func() { + defer wg.Done() + result[i] = cb(v, i) + }() + }(v, i) + } + wg.Wait() + return result +} -- cgit v1.2.3