Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding Earliest and Latest #468

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ Supported search helpers:
- [FindDuplicatesBy](#findduplicatesby)
- [Min](#min)
- [MinBy](#minby)
- [Earliest](#earliest)
- [Max](#max)
- [MaxBy](#maxby)
- [Latest](#latest)
- [Last](#last)
- [Nth](#nth)
- [Sample](#sample)
Expand Down Expand Up @@ -2045,7 +2047,7 @@ duplicatedValues := lo.FindDuplicatesBy([]int{3, 4, 5, 6, 7}, func(i int) int {

Search the minimum value of a collection.

Returns zero value when collection is empty.
Returns zero value when the collection is empty.

```go
min := lo.Min([]int{1, 2, 3})
Expand All @@ -2064,7 +2066,7 @@ 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 collection is empty.
Returns zero value when the collection is empty.

```go
min := lo.MinBy([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
Expand All @@ -2078,11 +2080,22 @@ min := lo.MinBy([]string{}, func(item string, min string) bool {
// ""
```

### Earliest

Search the minimum time.Time of a collection.

Returns zero value when the collection is empty.

```go
earliest := lo.Earliest(time.Now(), time.Time{})
// 0001-01-01 00:00:00 +0000 UTC
```

### Max

Search the maximum value of a collection.

Returns zero value when collection is empty.
Returns zero value when the collection is empty.

```go
max := lo.Max([]int{1, 2, 3})
Expand All @@ -2101,7 +2114,7 @@ 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 collection is empty.
Returns zero value when the collection is empty.

```go
max := lo.MaxBy([]string{"string1", "s2", "string3"}, func(item string, max string) bool {
Expand All @@ -2115,6 +2128,17 @@ max := lo.MaxBy([]string{}, func(item string, max string) bool {
// ""
```

### Latest

Search the maximum time.Time of a collection.

Returns zero value when the collection is empty.

```go
latest := lo.Latest([]time.Time{time.Now(), time.Time{}})
// 2023-04-01 01:02:03 +0000 UTC
```

### Last

Returns the last element of a collection or error if empty.
Expand Down
53 changes: 49 additions & 4 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lo
import (
"fmt"
"math/rand"
"time"

"golang.org/x/exp/constraints"
)
Expand Down Expand Up @@ -221,7 +222,7 @@ func FindDuplicatesBy[T any, U comparable](collection []T, iteratee func(item T)
}

// Min search the minimum value of a collection.
// Returns zero value when collection is empty.
// Returns zero value when the collection is empty.
func Min[T constraints.Ordered](collection []T) T {
var min T

Expand All @@ -244,7 +245,7 @@ func Min[T constraints.Ordered](collection []T) T {

// 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 collection is empty.
// Returns zero value when the collection is empty.
func MinBy[T any](collection []T, comparison func(a T, b T) bool) T {
var min T

Expand All @@ -265,8 +266,30 @@ func MinBy[T any](collection []T, comparison func(a T, b T) bool) T {
return min
}

// Earliest search the minimum time.Time of a collection.
// Returns zero value when the collection is empty.
func Earliest(times ...time.Time) time.Time {
var min time.Time

if len(times) == 0 {
return min
}

min = times[0]

for i := 1; i < len(times); i++ {
item := times[i]

if item.Before(min) {
min = item
}
}

return min
}

// Max searches the maximum value of a collection.
// Returns zero value when collection is empty.
// Returns zero value when the collection is empty.
func Max[T constraints.Ordered](collection []T) T {
var max T

Expand All @@ -289,7 +312,7 @@ func Max[T constraints.Ordered](collection []T) T {

// 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 collection is empty.
// Returns zero value when the collection is empty.
func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T {
var max T

Expand All @@ -310,6 +333,28 @@ func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T {
return max
}

// Latest search the maximum time.Time of a collection.
// Returns zero value when the collection is empty.
func Latest(times ...time.Time) time.Time {
var max time.Time

if len(times) == 0 {
return max
}

max = times[0]

for i := 1; i < len(times); i++ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason you use for I++ loop and not a range ?

Suggested change
for i := 1; i < len(times); i++ {
for _, item := range items {
if item.After(max) {
max = item
}
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To skip the first.

Comparing a zero value with item is not that safe.

item := times[i]

if item.After(max) {
max = item
}
}

return max
}

// Last returns the last element of a collection or error if empty.
func Last[T any](collection []T) (T, error) {
length := len(collection)
Expand Down
26 changes: 26 additions & 0 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,19 @@ func TestMinBy(t *testing.T) {
is.Equal(result3, "")
}

func TestEarliest(t *testing.T) {
t.Parallel()
is := assert.New(t)

a := time.Now()
b := a.Add(time.Hour)
result1 := Earliest(a, b)
result2 := Earliest()

is.Equal(result1, a)
is.Equal(result2, time.Time{})
}

func TestMax(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down Expand Up @@ -329,6 +342,19 @@ func TestMaxBy(t *testing.T) {
is.Equal(result3, "")
}

func TestLatest(t *testing.T) {
t.Parallel()
is := assert.New(t)

a := time.Now()
b := a.Add(time.Hour)
result1 := Latest(a, b)
result2 := Latest()

is.Equal(result1, b)
is.Equal(result2, time.Time{})
}

func TestLast(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down
Loading