github.com/webx-top/com@v1.2.12/slice_go1.18.go (about)

     1  //go:build go1.18
     2  
     3  package com
     4  
     5  import "sort"
     6  
     7  type Number interface {
     8  	~uint8 | ~int8 | ~uint16 | ~int16 | ~uint32 | ~int32 | ~uint | ~int | ~uint64 | ~int64 | ~float32 | ~float64
     9  }
    10  
    11  type Scalar interface {
    12  	Number | ~bool | ~string
    13  }
    14  
    15  func SliceExtractCallback[T Scalar](parts []string, cb func(string) T, recv ...*T) {
    16  	recvEndIndex := len(recv) - 1
    17  	if recvEndIndex < 0 {
    18  		return
    19  	}
    20  	for index, value := range parts {
    21  		if index > recvEndIndex {
    22  			break
    23  		}
    24  		*recv[index] = cb(value)
    25  	}
    26  }
    27  
    28  type reverseSortIndex[T any] []T
    29  
    30  func (s reverseSortIndex[T]) Len() int { return len(s) }
    31  func (s reverseSortIndex[T]) Less(i, j int) bool {
    32  	return j < i
    33  }
    34  func (s reverseSortIndex[T]) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    35  
    36  func ReverseSortIndex[T any](values []T) []T {
    37  	sort.Sort(reverseSortIndex[T](values))
    38  	return values
    39  }