@@ -102,11 +102,7 @@ func NoneBy[T any](collection []T, predicate func(item T) bool) bool {
102
102
// Play: https://go.dev/play/p/uuElL9X9e58
103
103
func Intersect [T comparable , Slice ~ []T ](list1 , list2 Slice ) Slice {
104
104
result := Slice {}
105
- seen := map [T ]struct {}{}
106
-
107
- for i := range list1 {
108
- seen [list1 [i ]] = struct {}{}
109
- }
105
+ seen := Keyify (list1 )
110
106
111
107
for i := range list2 {
112
108
if _ , ok := seen [list2 [i ]]; ok {
@@ -125,16 +121,8 @@ func Difference[T comparable, Slice ~[]T](list1, list2 Slice) (Slice, Slice) {
125
121
left := Slice {}
126
122
right := Slice {}
127
123
128
- seenLeft := map [T ]struct {}{}
129
- seenRight := map [T ]struct {}{}
130
-
131
- for i := range list1 {
132
- seenLeft [list1 [i ]] = struct {}{}
133
- }
134
-
135
- for i := range list2 {
136
- seenRight [list2 [i ]] = struct {}{}
137
- }
124
+ seenLeft := Keyify (list1 )
125
+ seenRight := Keyify (list2 )
138
126
139
127
for i := range list1 {
140
128
if _ , ok := seenRight [list1 [i ]]; ! ok {
@@ -179,10 +167,7 @@ func Union[T comparable, Slice ~[]T](lists ...Slice) Slice {
179
167
// Without returns a slice excluding all given values.
180
168
// Play: https://go.dev/play/p/5j30Ux8TaD0
181
169
func Without [T comparable , Slice ~ []T ](collection Slice , exclude ... T ) Slice {
182
- excludeMap := make (map [T ]struct {}, len (exclude ))
183
- for i := range exclude {
184
- excludeMap [exclude [i ]] = struct {}{}
185
- }
170
+ excludeMap := Keyify (exclude )
186
171
187
172
result := make (Slice , 0 , len (collection ))
188
173
for i := range collection {
@@ -197,10 +182,7 @@ func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice {
197
182
// Returns a new slice containing only the elements whose keys are not in the exclude list.
198
183
// Play: https://go.dev/play/p/VgWJOF01NbJ
199
184
func WithoutBy [T any , K comparable , Slice ~ []T ](collection Slice , iteratee func (item T ) K , exclude ... K ) Slice {
200
- excludeMap := make (map [K ]struct {}, len (exclude ))
201
- for _ , e := range exclude {
202
- excludeMap [e ] = struct {}{}
203
- }
185
+ excludeMap := Keyify (exclude )
204
186
205
187
result := make (Slice , 0 , len (collection ))
206
188
for _ , item := range collection {
@@ -221,14 +203,7 @@ func WithoutEmpty[T comparable, Slice ~[]T](collection Slice) Slice {
221
203
// WithoutNth returns a slice excluding the nth value.
222
204
// Play: https://go.dev/play/p/5g3F9R2H1xL
223
205
func WithoutNth [T comparable , Slice ~ []T ](collection Slice , nths ... int ) Slice {
224
- length := len (collection )
225
-
226
- toRemove := make (map [int ]struct {}, len (nths ))
227
- for i := range nths {
228
- if nths [i ] >= 0 && nths [i ] <= length - 1 {
229
- toRemove [nths [i ]] = struct {}{}
230
- }
231
- }
206
+ toRemove := Keyify (nths )
232
207
233
208
result := make (Slice , 0 , len (collection ))
234
209
for i := range collection {
0 commit comments