refactor: use contraints

This commit is contained in:
Ruidy 2022-01-01 14:21:05 -04:00
parent a0944476b9
commit 7ddb05602c
4 changed files with 9 additions and 8 deletions

View file

@ -1,7 +1,9 @@
package underscore
import "constraints"
// Contains returns true if the value is present in the slice
func Contains[T numbers](values []T, value T) bool {
func Contains[T constraints.Ordered](values []T, value T) bool {
for _, v := range values {
if v == value {
return true

4
max.go
View file

@ -1,9 +1,11 @@
package underscore
import "constraints"
// Max returns the maximum value in the slice.
// This function can currently only compare numbers reliably.
// This function uses operator <.
func Max[T numbers](values []T) T {
func Max[T constraints.Ordered](values []T) T {
max := values[0]
for _, v := range values {
if v > max {

4
min.go
View file

@ -1,9 +1,11 @@
package underscore
import "constraints"
// Min returns the minimum value in the slice.
// This function can currently only compare numbers reliably.
// This function uses operator <.
func Min[T numbers](values []T) T {
func Min[T constraints.Ordered](values []T) T {
min := values[0]
for _, v := range values {
if v < min {

View file

@ -1,5 +0,0 @@
package underscore
type numbers interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}