Skip to content

Commit b000c2a

Browse files
authored
feat: use map to find in slice
1 parent 6e5ec05 commit b000c2a

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

mapx/mapx.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package mapx
22

3-
import (
4-
"github.com/zeiss/pkg/slices"
5-
)
3+
import "github.com/zeiss/pkg/slices"
64

75
// Delete removes elements from a map by key.
86
func Delete[T1 comparable, T2 any](m map[T1]T2, keys ...T1) {

slices/slices.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package slices
22

3-
import "github.com/zeiss/pkg/cast"
3+
import (
4+
"github.com/zeiss/pkg/cast"
5+
)
46

57
// Any checks if any element in a slice satisfies a predicate.
68
func Any[T any](fn func(v T) bool, slice ...T) bool {
@@ -106,13 +108,13 @@ func Last[T any](slice ...T) T {
106108

107109
// In checks if a value is in a slice.
108110
func In[T comparable](val T, slice ...T) bool {
111+
m := make(map[T]bool, len(slice))
109112
for _, v := range slice {
110-
if v == val {
111-
return true
112-
}
113+
m[v] = true
113114
}
114115

115-
return false
116+
_, ok := m[val]
117+
return ok
116118
}
117119

118120
// Index returns the index of the first element in a slice that satisfies a predicate.

0 commit comments

Comments
 (0)