github.com/wfusion/gofusion@v1.1.14/common/utils/slice.go (about)

     1  package utils
     2  
     3  func SliceRemove[T any, TS ~[]T](s TS, filter func(t T) bool) (d TS) {
     4  	for i := len(s) - 1; i >= 0; i-- {
     5  		if filter(s[i]) {
     6  			s = append(s[:i], s[i+1:]...)
     7  		}
     8  	}
     9  	return s
    10  }
    11  
    12  func SliceReverse[T any, TS ~[]T](s TS) {
    13  	for i := len(s)/2 - 1; i >= 0; i-- {
    14  		opp := len(s) - 1 - i
    15  		s[i], s[opp] = s[opp], s[i]
    16  	}
    17  }
    18  
    19  // SliceSplit Separate objects into several size
    20  func SliceSplit[T any, TS ~[]T](arr TS, size int) []TS {
    21  	if size <= 0 {
    22  		return []TS{arr}
    23  	}
    24  
    25  	chunkSet := make([]TS, 0, len(arr)/size+1)
    26  
    27  	var chunk TS
    28  	for len(arr) > size {
    29  		chunk, arr = arr[:size], arr[size:]
    30  		chunkSet = append(chunkSet, chunk)
    31  	}
    32  	if len(arr) > 0 {
    33  		chunkSet = append(chunkSet, arr[:])
    34  	}
    35  
    36  	return chunkSet
    37  }