mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
refactor: use contraints
This commit is contained in:
parent
a0944476b9
commit
7ddb05602c
4 changed files with 9 additions and 8 deletions
|
|
@ -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
4
max.go
|
|
@ -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
4
min.go
|
|
@ -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 {
|
||||
|
|
|
|||
5
types.go
5
types.go
|
|
@ -1,5 +0,0 @@
|
|||
package underscore
|
||||
|
||||
type numbers interface {
|
||||
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
|
||||
}
|
||||
Loading…
Reference in a new issue