From eddfb60d44c42fb50fe31c20001043b483907287 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 29 Dec 2021 22:30:57 -0400 Subject: [PATCH] feat: max --- README.md | 1 + contains.go | 4 ---- max.go | 11 +++++++++++ max_test.go | 15 +++++++++++++++ types.go | 5 +++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 max.go create mode 100644 max_test.go create mode 100644 types.go diff --git a/README.md b/README.md index 208be6c..0627ad1 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/contains.go b/contains.go index ac66b96..3d2102f 100644 --- a/contains.go +++ b/contains.go @@ -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 { diff --git a/max.go b/max.go new file mode 100644 index 0000000..9821e82 --- /dev/null +++ b/max.go @@ -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 +} diff --git a/max_test.go b/max_test.go new file mode 100644 index 0000000..5fed20a --- /dev/null +++ b/max_test.go @@ -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)) +} diff --git a/types.go b/types.go new file mode 100644 index 0000000..50c6345 --- /dev/null +++ b/types.go @@ -0,0 +1,5 @@ +package underscore + +type numbers interface { + int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 +}