mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
feat: min
This commit is contained in:
parent
eddfb60d44
commit
b26a360495
4 changed files with 33 additions and 0 deletions
|
|
@ -20,3 +20,4 @@ It is mostly a port from the `underscore.js` library based on generics brought b
|
|||
- `find`
|
||||
- `contains` (only numerics values at the moment)
|
||||
- `max`
|
||||
- `min`
|
||||
|
|
|
|||
3
max.go
3
max.go
|
|
@ -1,5 +1,8 @@
|
|||
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 {
|
||||
max := values[0]
|
||||
for _, v := range values {
|
||||
|
|
|
|||
14
min.go
Normal file
14
min.go
Normal 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
15
min_test.go
Normal 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))
|
||||
}
|
||||
Loading…
Reference in a new issue