1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
}
|