feat: min

This commit is contained in:
Ruidy 2021-12-29 22:35:39 -04:00
parent eddfb60d44
commit b26a360495
4 changed files with 33 additions and 0 deletions

View file

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

3
max.go
View file

@ -1,5 +1,8 @@
package underscore package underscore
// 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 numbers](values []T) T {
max := values[0] max := values[0]
for _, v := range values { for _, v := range values {

14
min.go Normal file
View file

@ -0,0 +1,14 @@
package underscore
// 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 {
min := values[0]
for _, v := range values {
if v < min {
min = v
}
}
return min
}

15
min_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 TestMin(t *testing.T) {
nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5}
want := 1
assert.Equal(t, want, u.Min(nums))
}