Skip to content

Commit

Permalink
feat: add (Min|Max)Index(By) (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
aria3ppp authored Jan 24, 2025
1 parent 5512d3a commit a6a6a82
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 0 deletions.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,15 @@ Supported search helpers:
- [FindDuplicates](#findduplicates)
- [FindDuplicatesBy](#findduplicatesby)
- [Min](#min)
- [MinIndex](#minindex)
- [MinBy](#minby)
- [MinIndexBy](#minindexby)
- [Earliest](#earliest)
- [EarliestBy](#earliestby)
- [Max](#max)
- [MaxIndex](#maxindex)
- [MaxBy](#maxby)
- [MaxIndexBy](#maxindexby)
- [Latest](#latest)
- [LatestBy](#latestby)
- [First](#first)
Expand Down Expand Up @@ -2285,6 +2289,23 @@ min := lo.Min([]time.Duration{time.Second, time.Hour})
// 1s
```

### MinIndex

Search the minimum value of a collection and the index of the minimum value.

Returns (zero value, -1) when the collection is empty.

```go
min, index := lo.MinIndex([]int{1, 2, 3})
// 1, 0

min, index := lo.MinIndex([]int{})
// 0, -1

min, index := lo.MinIndex([]time.Duration{time.Second, time.Hour})
// 1s, 0
```

### MinBy

Search the minimum value of a collection using the given comparison function.
Expand All @@ -2305,6 +2326,26 @@ min := lo.MinBy([]string{}, func(item string, min string) bool {
// ""
```

### MinIndexBy

Search the minimum value of a collection using the given comparison function and the index of the minimum value.

If several values of the collection are equal to the smallest value, returns the first such value.

Returns (zero value, -1) when the collection is empty.

```go
min, index := lo.MinIndexBy([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
return len(item) < len(min)
})
// "s1", 0

min, index := lo.MinIndexBy([]string{}, func(item string, min string) bool {
return len(item) < len(min)
})
// "", -1
```

### Earliest

Search the minimum time.Time of a collection.
Expand Down Expand Up @@ -2350,6 +2391,23 @@ max := lo.Max([]time.Duration{time.Second, time.Hour})
// 1h
```

### MaxIndex

Search the maximum value of a collection and the index of the maximum value.

Returns (zero value, -1) when the collection is empty.

```go
max, index := lo.MaxIndex([]int{1, 2, 3})
// 3, 2

max, index := lo.MaxIndex([]int{})
// 0, -1

max, index := lo.MaxIndex([]time.Duration{time.Second, time.Hour})
// 1h, 1
```

### MaxBy

Search the maximum value of a collection using the given comparison function.
Expand All @@ -2370,6 +2428,26 @@ max := lo.MaxBy([]string{}, func(item string, max string) bool {
// ""
```

### MaxIndexBy

Search the maximum value of a collection using the given comparison function and the index of the maximum value.

If several values of the collection are equal to the greatest value, returns the first such value.

Returns (zero value, -1) when the collection is empty.

```go
max, index := lo.MaxIndexBy([]string{"string1", "s2", "string3"}, func(item string, max string) bool {
return len(item) > len(max)
})
// "string1", 0

max, index := lo.MaxIndexBy([]string{}, func(item string, max string) bool {
return len(item) > len(max)
})
// "", -1
```

### Latest

Search the maximum time.Time of a collection.
Expand Down
106 changes: 106 additions & 0 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,32 @@ func Min[T constraints.Ordered](collection []T) T {
return min
}

// MinIndex search the minimum value of a collection and the index of the minimum value.
// Returns (zero value, -1) when the collection is empty.
func MinIndex[T constraints.Ordered](collection []T) (T, int) {
var (
min T
index int
)

if len(collection) == 0 {
return min, -1
}

min = collection[0]

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

if item < min {
min = item
index = i
}
}

return min, index
}

// 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.
Expand All @@ -264,6 +290,33 @@ func MinBy[T any](collection []T, comparison func(a T, b T) bool) T {
return min
}

// MinIndexBy search the minimum value of a collection using the given comparison function and the index of the minimum value.
// If several values of the collection are equal to the smallest value, returns the first such value.
// Returns (zero value, -1) when the collection is empty.
func MinIndexBy[T any](collection []T, comparison func(a T, b T) bool) (T, int) {
var (
min T
index int
)

if len(collection) == 0 {
return min, -1
}

min = collection[0]

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

if comparison(item, min) {
min = item
index = i
}
}

return min, index
}

// Earliest search the minimum time.Time of a collection.
// Returns zero value when the collection is empty.
func Earliest(times ...time.Time) time.Time {
Expand Down Expand Up @@ -332,6 +385,32 @@ func Max[T constraints.Ordered](collection []T) T {
return max
}

// MaxIndex searches the maximum value of a collection and the index of the maximum value.
// Returns (zero value, -1) when the collection is empty.
func MaxIndex[T constraints.Ordered](collection []T) (T, int) {
var (
max T
index int
)

if len(collection) == 0 {
return max, -1
}

max = collection[0]

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

if item > max {
max = item
index = i
}
}

return max, index
}

// 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.
Expand All @@ -355,6 +434,33 @@ func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T {
return max
}

// MaxIndexBy search the maximum value of a collection using the given comparison function and the index of the maximum value.
// If several values of the collection are equal to the greatest value, returns the first such value.
// Returns (zero value, -1) when the collection is empty.
func MaxIndexBy[T any](collection []T, comparison func(a T, b T) bool) (T, int) {
var (
max T
index int
)

if len(collection) == 0 {
return max, -1
}

max = collection[0]

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

if comparison(item, max) {
max = item
index = i
}
}

return max, index
}

// Latest search the maximum time.Time of a collection.
// Returns zero value when the collection is empty.
func Latest(times ...time.Time) time.Time {
Expand Down
92 changes: 92 additions & 0 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,28 @@ func TestMin(t *testing.T) {
is.Equal(result4, 0)
}

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

result1, index1 := MinIndex([]int{1, 2, 3})
result2, index2 := MinIndex([]int{3, 2, 1})
result3, index3 := MinIndex([]time.Duration{time.Second, time.Minute, time.Hour})
result4, index4 := MinIndex([]int{})

is.Equal(result1, 1)
is.Equal(index1, 0)

is.Equal(result2, 1)
is.Equal(index2, 2)

is.Equal(result3, time.Second)
is.Equal(index3, 0)

is.Equal(result4, 0)
is.Equal(index4, -1)
}

func TestMinBy(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand All @@ -323,6 +345,30 @@ func TestMinBy(t *testing.T) {
is.Equal(result3, "")
}

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

result1, index1 := MinIndexBy([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
return len(item) < len(min)
})
result2, index2 := MinIndexBy([]string{"string1", "string2", "s3"}, func(item string, min string) bool {
return len(item) < len(min)
})
result3, index3 := MinIndexBy([]string{}, func(item string, min string) bool {
return len(item) < len(min)
})

is.Equal(result1, "s1")
is.Equal(index1, 0)

is.Equal(result2, "s3")
is.Equal(index2, 2)

is.Equal(result3, "")
is.Equal(index3, -1)
}

func TestEarliest(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down Expand Up @@ -377,6 +423,28 @@ func TestMax(t *testing.T) {
is.Equal(result4, 0)
}

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

result1, index1 := MaxIndex([]int{1, 2, 3})
result2, index2 := MaxIndex([]int{3, 2, 1})
result3, index3 := MaxIndex([]time.Duration{time.Second, time.Minute, time.Hour})
result4, index4 := MaxIndex([]int{})

is.Equal(result1, 3)
is.Equal(index1, 2)

is.Equal(result2, 3)
is.Equal(index2, 0)

is.Equal(result3, time.Hour)
is.Equal(index3, 2)

is.Equal(result4, 0)
is.Equal(index4, -1)
}

func TestMaxBy(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand All @@ -396,6 +464,30 @@ func TestMaxBy(t *testing.T) {
is.Equal(result3, "")
}

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

result1, index1 := MaxIndexBy([]string{"s1", "string2", "s3"}, func(item string, max string) bool {
return len(item) > len(max)
})
result2, index2 := MaxIndexBy([]string{"string1", "string2", "s3"}, func(item string, max string) bool {
return len(item) > len(max)
})
result3, index3 := MaxIndexBy([]string{}, func(item string, max string) bool {
return len(item) > len(max)
})

is.Equal(result1, "string2")
is.Equal(index1, 1)

is.Equal(result2, "string1")
is.Equal(index2, 0)

is.Equal(result3, "")
is.Equal(index3, -1)
}

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

0 comments on commit a6a6a82

Please sign in to comment.