...

Source file src/oss.terrastruct.com/util-go/go2/go2.go

Documentation: oss.terrastruct.com/util-go/go2

     1  // Package go2 contains general utility helpers that should've been in Go. Maybe they'll be in Go 2.0.
     2  package go2
     3  
     4  import (
     5  	"hash/fnv"
     6  	"math"
     7  
     8  	"golang.org/x/exp/constraints"
     9  )
    10  
    11  func Pointer[T any](v T) *T {
    12  	return &v
    13  }
    14  
    15  func Min[T constraints.Ordered](a, b T) T {
    16  	if a < b {
    17  		return a
    18  	}
    19  	return b
    20  }
    21  
    22  func Max[T constraints.Ordered](a, b T) T {
    23  	if a > b {
    24  		return a
    25  	}
    26  	return b
    27  }
    28  
    29  func StringToIntHash(s string) int {
    30  	h := fnv.New32a()
    31  	h.Write([]byte(s))
    32  	return int(h.Sum32())
    33  }
    34  
    35  func Contains[T comparable](els []T, el T) bool {
    36  	for _, el2 := range els {
    37  		if el2 == el {
    38  			return true
    39  		}
    40  	}
    41  	return false
    42  }
    43  
    44  func Filter[T any](els []T, fn func(T) bool) []T {
    45  	out := []T{}
    46  	for _, el := range els {
    47  		if fn(el) {
    48  			out = append(out, el)
    49  		}
    50  	}
    51  	return out
    52  }
    53  
    54  func IntMax(x, y int) int {
    55  	return int(math.Max(float64(x), float64(y)))
    56  }
    57  
    58  func IntMin(x, y int) int {
    59  	return int(math.Min(float64(x), float64(y)))
    60  }
    61  

View as plain text