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

     1  package utils
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  
     6  	"github.com/wfusion/gofusion/common/constraint"
     7  )
     8  
     9  var (
    10  	ErrEmptyArray = errors.New("empty array")
    11  )
    12  
    13  func Max[T constraint.Sortable](arr ...T) T {
    14  	if len(arr) == 0 {
    15  		panic(ErrEmptyArray)
    16  	}
    17  
    18  	max := arr[0]
    19  	for i := 1; i < len(arr); i++ {
    20  		if arr[i] > max {
    21  			max = arr[i]
    22  		}
    23  	}
    24  
    25  	return max
    26  }
    27  
    28  func Min[T constraint.Sortable](arr ...T) T {
    29  	if len(arr) == 0 {
    30  		panic(ErrEmptyArray)
    31  	}
    32  
    33  	min := arr[0]
    34  	for i := 1; i < len(arr); i++ {
    35  		if arr[i] < min {
    36  			min = arr[i]
    37  		}
    38  	}
    39  
    40  	return min
    41  }
    42  
    43  func IsInRange[T constraint.Sortable](num, min, max T) bool {
    44  	if num < min || num > max {
    45  		return false
    46  	}
    47  	return true
    48  }