Skip to content

Commit

Permalink
feat: use map to find in slice
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Oct 25, 2024
1 parent 6e5ec05 commit b000c2a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 1 addition & 3 deletions mapx/mapx.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mapx

import (
"github.com/zeiss/pkg/slices"
)
import "github.com/zeiss/pkg/slices"

// Delete removes elements from a map by key.
func Delete[T1 comparable, T2 any](m map[T1]T2, keys ...T1) {
Expand Down
12 changes: 7 additions & 5 deletions slices/slices.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package slices

import "github.com/zeiss/pkg/cast"
import (
"github.com/zeiss/pkg/cast"
)

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

// In checks if a value is in a slice.
func In[T comparable](val T, slice ...T) bool {
m := make(map[T]bool, len(slice))
for _, v := range slice {
if v == val {
return true
}
m[v] = true
}

return false
_, ok := m[val]
return ok
}

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

0 comments on commit b000c2a

Please sign in to comment.