mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
feat: max
This commit is contained in:
parent
0f07a76950
commit
eddfb60d44
5 changed files with 32 additions and 4 deletions
|
|
@ -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`
|
||||
|
|
|
|||
|
|
@ -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
11
max.go
Normal 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
15
max_test.go
Normal 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
5
types.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package underscore
|
||||
|
||||
type numbers interface {
|
||||
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
|
||||
}
|
||||
Loading…
Reference in a new issue