@@ -7,17 +7,14 @@ import "sync"
7
7
func Map [T any , R any ](collection []T , iteratee func (T , int ) R ) []R {
8
8
result := make ([]R , len (collection ))
9
9
10
- var mu sync.Mutex
11
10
var wg sync.WaitGroup
12
11
wg .Add (len (collection ))
13
12
14
13
for i , item := range collection {
15
- go func (_item T , _i int ) {
14
+ go func (_item T , _i int ) {
16
15
res := iteratee (_item , _i )
17
16
18
- mu .Lock ()
19
17
result [_i ] = res
20
- mu .Unlock ()
21
18
22
19
wg .Done ()
23
20
}(item , i )
@@ -35,7 +32,7 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
35
32
wg .Add (len (collection ))
36
33
37
34
for i , item := range collection {
38
- go func (_item T , _i int ) {
35
+ go func (_item T , _i int ) {
39
36
iteratee (_item , _i )
40
37
wg .Done ()
41
38
}(item , i )
@@ -50,17 +47,14 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
50
47
func Times [T any ](count int , iteratee func (int ) T ) []T {
51
48
result := make ([]T , count )
52
49
53
- var mu sync.Mutex
54
50
var wg sync.WaitGroup
55
51
wg .Add (count )
56
52
57
53
for i := 0 ; i < count ; i ++ {
58
54
go func (_i int ) {
59
55
item := iteratee (_i )
60
56
61
- mu .Lock ()
62
57
result [_i ] = item
63
- mu .Unlock ()
64
58
65
59
wg .Done ()
66
60
}(i )
@@ -81,7 +75,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
81
75
wg .Add (len (collection ))
82
76
83
77
for _ , item := range collection {
84
- go func (_item T ) {
78
+ go func (_item T ) {
85
79
key := iteratee (_item )
86
80
87
81
mu .Lock ()
@@ -91,7 +85,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
91
85
}
92
86
93
87
result [key ] = append (result [key ], _item )
94
-
88
+
95
89
mu .Unlock ()
96
90
wg .Done ()
97
91
}(item )
@@ -106,7 +100,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
106
100
// determined by the order they occur in collection. The grouping is generated from the results
107
101
// of running each element of collection through iteratee.
108
102
// `iteratee` is call in parallel.
109
- func PartitionBy [T any , K comparable ](collection []T , iteratee func (x T ) K ) [][]T {
103
+ func PartitionBy [T any , K comparable ](collection []T , iteratee func (x T ) K ) [][]T {
110
104
result := [][]T {}
111
105
seen := map [K ]int {}
112
106
0 commit comments