gitee.com/quant1x/gox@v1.21.2/api/slices.go (about)

     1  package api
     2  
     3  import "slices"
     4  
     5  // Reverse 反转切片
     6  func Reverse[S ~[]E, E any](s S) S {
     7  	d := slices.Clone(s)
     8  	for i, j := 0, len(d)-1; i < j; i, j = i+1, j-1 {
     9  		d[i], d[j] = d[j], d[i]
    10  	}
    11  	return d
    12  }
    13  
    14  // Filter slice过滤
    15  func Filter[S ~[]E, E any](slice S, condition func(E) bool) S {
    16  	var filtered []E
    17  	for _, item := range slice {
    18  		if condition(item) {
    19  			filtered = append(filtered, item)
    20  		}
    21  	}
    22  	return filtered
    23  }