From 1bc00cdea42105c865e4cf4ce062d4f163ae55bc Mon Sep 17 00:00:00 2001 From: "zhaowenjie.kevin" Date: Tue, 12 Nov 2024 16:36:46 +0800 Subject: [PATCH 1/2] implement maxn & minn --- find.go | 12 ++++++++++++ find_test.go | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/find.go b/find.go index 59c23460..b45addbd 100644 --- a/find.go +++ b/find.go @@ -241,6 +241,12 @@ func Min[T constraints.Ordered](collection []T) T { return min } +// MinN searches the minimum value from varidical items. +// Returns zero value when the no items are given. +func MinN[T constraints.Ordered](items ...T) T { + return Min(items) +} + // MinBy search the minimum value of a collection using the given comparison function. // If several values of the collection are equal to the smallest value, returns the first such value. // Returns zero value when the collection is empty. @@ -332,6 +338,12 @@ func Max[T constraints.Ordered](collection []T) T { return max } +// MaxN searches the maximum value from varidical items. +// Returns zero value when the no items are given. +func MaxN[T constraints.Ordered](items ...T) T { + return Max(items) +} + // MaxBy search the maximum value of a collection using the given comparison function. // If several values of the collection are equal to the greatest value, returns the first such value. // Returns zero value when the collection is empty. diff --git a/find_test.go b/find_test.go index b1533997..feeb2199 100644 --- a/find_test.go +++ b/find_test.go @@ -302,6 +302,16 @@ func TestMin(t *testing.T) { is.Equal(result2, 1) is.Equal(result3, time.Second) is.Equal(result4, 0) + + result1 = MinN(1, 2, 3) + result2 = MinN(3, 2, 1) + result3 = MinN(time.Second, time.Minute, time.Hour) + result4 = MinN([]int{}...) + + is.Equal(result1, 1) + is.Equal(result2, 1) + is.Equal(result3, time.Second) + is.Equal(result4, 0) } func TestMinBy(t *testing.T) { @@ -375,6 +385,16 @@ func TestMax(t *testing.T) { is.Equal(result2, 3) is.Equal(result3, time.Hour) is.Equal(result4, 0) + + result1 = MaxN(1, 2, 3) + result2 = MaxN(3, 2, 1) + result3 = MaxN(time.Second, time.Minute, time.Hour) + result4 = MaxN([]int{}...) + + is.Equal(result1, 3) + is.Equal(result2, 3) + is.Equal(result3, time.Hour) + is.Equal(result4, 0) } func TestMaxBy(t *testing.T) { From e1c0af2445b248d3ebe532eef888a43a75c6e226 Mon Sep 17 00:00:00 2001 From: "zhaowenjie.kevin" Date: Tue, 12 Nov 2024 16:51:36 +0800 Subject: [PATCH 2/2] update readme --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ find.go | 8 ++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 73c33223..080d567c 100644 --- a/README.md +++ b/README.md @@ -223,10 +223,12 @@ Supported search helpers: - [FindDuplicates](#findduplicates) - [FindDuplicatesBy](#findduplicatesby) - [Min](#min) +- [MinN](#minn) - [MinBy](#minby) - [Earliest](#earliest) - [EarliestBy](#earliestby) - [Max](#max) +- [MaxN](#maxn) - [MaxBy](#maxby) - [Latest](#latest) - [LatestBy](#latestby) @@ -2285,6 +2287,23 @@ min := lo.Min([]time.Duration{time.Second, time.Hour}) // 1s ``` +### MinN + +Search the minimum value of arbitrary number params. + +Returns zero value when the no param are given. + +```go +min := lo.MinN(1, 2, 3) +// 1 + +min := lo.Min([]int{}...) +// 0 + +min := lo.Min(time.Second, time.Hour...) +// 1s +``` + ### MinBy Search the minimum value of a collection using the given comparison function. @@ -2350,6 +2369,25 @@ max := lo.Max([]time.Duration{time.Second, time.Hour}) // 1h ``` +### MaxN + +Search the maximum value of arbitrary number params. + +Returns zero value when the no param are given. + +```go +max := lo.MaxN(1, 2, 3) +// 3 + +max := lo.MaxN([]int{}...) +// 0 + +max := lo.MaxN(time.Second, time.Hour) +// 1h +``` + + + ### MaxBy Search the maximum value of a collection using the given comparison function. diff --git a/find.go b/find.go index b45addbd..a7ec7f15 100644 --- a/find.go +++ b/find.go @@ -241,8 +241,8 @@ func Min[T constraints.Ordered](collection []T) T { return min } -// MinN searches the minimum value from varidical items. -// Returns zero value when the no items are given. +// MinN search the minimum value of arbitrary number params. +// returns zero value when the no param are given. func MinN[T constraints.Ordered](items ...T) T { return Min(items) } @@ -338,8 +338,8 @@ func Max[T constraints.Ordered](collection []T) T { return max } -// MaxN searches the maximum value from varidical items. -// Returns zero value when the no items are given. +// MinN searches the maximum value of arbitrary number params. +// returns zero value when the no param are given. func MaxN[T constraints.Ordered](items ...T) T { return Max(items) }