Skip to content

Commit 0df8d7b

Browse files
committed
lint: use Keyify internally
1 parent 43cef1f commit 0df8d7b

File tree

1 file changed

+6
-31
lines changed

1 file changed

+6
-31
lines changed

intersect.go

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ func NoneBy[T any](collection []T, predicate func(item T) bool) bool {
102102
// Play: https://go.dev/play/p/uuElL9X9e58
103103
func Intersect[T comparable, Slice ~[]T](list1, list2 Slice) Slice {
104104
result := Slice{}
105-
seen := map[T]struct{}{}
106-
107-
for i := range list1 {
108-
seen[list1[i]] = struct{}{}
109-
}
105+
seen := Keyify(list1)
110106

111107
for i := range list2 {
112108
if _, ok := seen[list2[i]]; ok {
@@ -125,16 +121,8 @@ func Difference[T comparable, Slice ~[]T](list1, list2 Slice) (Slice, Slice) {
125121
left := Slice{}
126122
right := Slice{}
127123

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)
138126

139127
for i := range list1 {
140128
if _, ok := seenRight[list1[i]]; !ok {
@@ -179,10 +167,7 @@ func Union[T comparable, Slice ~[]T](lists ...Slice) Slice {
179167
// Without returns a slice excluding all given values.
180168
// Play: https://go.dev/play/p/5j30Ux8TaD0
181169
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)
186171

187172
result := make(Slice, 0, len(collection))
188173
for i := range collection {
@@ -197,10 +182,7 @@ func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice {
197182
// Returns a new slice containing only the elements whose keys are not in the exclude list.
198183
// Play: https://go.dev/play/p/VgWJOF01NbJ
199184
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)
204186

205187
result := make(Slice, 0, len(collection))
206188
for _, item := range collection {
@@ -221,14 +203,7 @@ func WithoutEmpty[T comparable, Slice ~[]T](collection Slice) Slice {
221203
// WithoutNth returns a slice excluding the nth value.
222204
// Play: https://go.dev/play/p/5g3F9R2H1xL
223205
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)
232207

233208
result := make(Slice, 0, len(collection))
234209
for i := range collection {

0 commit comments

Comments
 (0)