...

Source file src/edge-infra.dev/pkg/edge/datasync/internal/collections/collections.go

Documentation: edge-infra.dev/pkg/edge/datasync/internal/collections

     1  package collections
     2  
     3  // Index returns the first index of the target string t, or -1 if no match is found.
     4  func Index(vs []string, t string) int {
     5  	for i, v := range vs {
     6  		if v == t {
     7  			return i
     8  		}
     9  	}
    10  	return -1
    11  }
    12  
    13  // Include returns true if the target string t is in the slice.
    14  func Include(vs []string, t string) bool {
    15  	return Index(vs, t) >= 0
    16  }
    17  
    18  // Any returns true if one of the strings in the slice satisfies the predicate f.
    19  func Any(vs []string, f func(string) bool) bool {
    20  	for _, v := range vs {
    21  		if f(v) {
    22  			return true
    23  		}
    24  	}
    25  	return false
    26  }
    27  
    28  // All returns true if all of the strings in the slice satisfy the predicate f.
    29  func All(vs []string, f func(string) bool) bool {
    30  	for _, v := range vs {
    31  		if !f(v) {
    32  			return false
    33  		}
    34  	}
    35  	return true
    36  }
    37  
    38  // Filter returns a new slice containing all strings in the slice that satisfy the predicate f.
    39  func Filter(vs []string, f func(string) bool) []string {
    40  	vsf := make([]string, 0)
    41  	for _, v := range vs {
    42  		if f(v) {
    43  			vsf = append(vsf, v)
    44  		}
    45  	}
    46  	return vsf
    47  }
    48  
    49  // Map returns a new slice containing the results of applying the function f to each string in the original slice.
    50  func Map(vs []string, f func(string) string) []string {
    51  	vsm := make([]string, len(vs))
    52  	for i, v := range vs {
    53  		vsm[i] = f(v)
    54  	}
    55  	return vsm
    56  }
    57  

View as plain text