feat: max

This commit is contained in:
Ruidy 2021-12-29 22:30:57 -04:00
parent 0f07a76950
commit eddfb60d44
5 changed files with 32 additions and 4 deletions

View file

@ -19,3 +19,4 @@ It is mostly a port from the `underscore.js` library based on generics brought b
- `every`
- `find`
- `contains` (only numerics values at the moment)
- `max`

View file

@ -1,9 +1,5 @@
package underscore
type numbers interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
func Contains[T numbers](values []T, value T) bool {
for _, v := range values {
if v == value {

11
max.go Normal file
View file

@ -0,0 +1,11 @@
package underscore
func Max[T numbers](values []T) T {
max := values[0]
for _, v := range values {
if v > max {
max = v
}
}
return max
}

15
max_test.go Normal file
View file

@ -0,0 +1,15 @@
package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestMax(t *testing.T) {
nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5}
want := 9
assert.Equal(t, want, u.Max(nums))
}

5
types.go Normal file
View file

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