github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/utils/list.go (about)

     1  package utils
     2  
     3  // FindInSlice finds an item in a slice and returns the
     4  // index. It will return -1 if not found
     5  func FindInSlice[T comparable](items []T, x T) int {
     6  	for idx, value := range items {
     7  		if value == x {
     8  			return idx
     9  		}
    10  	}
    11  
    12  	return -1
    13  }
    14  
    15  func FindAnyWith[T any](items []T, matchFn func(item *T) bool) *T {
    16  	var t *T
    17  
    18  	for _, item := range items {
    19  		if matchFn(&item) {
    20  			t = &item
    21  			break
    22  		}
    23  	}
    24  
    25  	return t
    26  }