Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3057,6 +3057,8 @@ max := lo.MaxBy([]string{}, func(item string, max string) bool {
// ""
```

[[play](https://go.dev/play/p/JW1qu-ECwF7)]

### MaxIndexBy

Search the maximum value of a collection using the given comparison function and the index of the maximum value.
Expand All @@ -3077,6 +3079,8 @@ max, index := lo.MaxIndexBy([]string{}, func(item string, max string) bool {
// "", -1
```

[[play](https://go.dev/play/p/uaUszc-c9QK)]

### Latest

Search the maximum time.Time of a collection.
Expand Down
4 changes: 3 additions & 1 deletion docs/data/core-maxby.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ slug: maxby
sourceRef: find.go#L459
category: core
subCategory: find
playUrl:
playUrl: https://go.dev/play/p/JW1qu-ECwF7
variantHelpers:
- core#find#maxby
similarHelpers:
Expand Down Expand Up @@ -34,4 +34,6 @@ max := lo.MaxBy([]Point{{1}, {5}, {3}}, func(a, b Point) bool {
// {5}
```

Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention.

See https://github.com/samber/lo/issues/129
4 changes: 3 additions & 1 deletion docs/data/core-maxindexby.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ slug: maxindexby
sourceRef: find.go#L482
category: core
subCategory: find
playUrl:
playUrl: https://go.dev/play/p/uaUszc-c9QK
variantHelpers:
- core#find#maxindexby
similarHelpers:
Expand Down Expand Up @@ -34,4 +34,6 @@ value, idx := lo.MaxIndexBy([]Point{{1}, {5}, {3}}, func(a, b Point) bool {
// value == {5}, idx == 1
```

Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention.

See https://github.com/samber/lo/issues/129
26 changes: 18 additions & 8 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func MinIndex[T constraints.Ordered](collection []T) (T, int) {
// 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.
func MinBy[T any](collection []T, comparison func(a, b T) bool) T {
func MinBy[T any](collection []T, less func(a, b T) bool) T {
var mIn T

if len(collection) == 0 {
Expand All @@ -323,7 +323,7 @@ func MinBy[T any](collection []T, comparison func(a, b T) bool) T {
for i := 1; i < len(collection); i++ {
item := collection[i]

if comparison(item, mIn) {
if less(item, mIn) {
mIn = item
}
}
Expand All @@ -334,7 +334,7 @@ func MinBy[T any](collection []T, comparison func(a, b T) bool) T {
// 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, b T) bool) (T, int) {
func MinIndexBy[T any](collection []T, less func(a, b T) bool) (T, int) {
var (
mIn T
index int
Expand All @@ -349,7 +349,7 @@ func MinIndexBy[T any](collection []T, comparison func(a, b T) bool) (T, int) {
for i := 1; i < len(collection); i++ {
item := collection[i]

if comparison(item, mIn) {
if less(item, mIn) {
mIn = item
index = i
}
Expand Down Expand Up @@ -456,7 +456,12 @@ func MaxIndex[T constraints.Ordered](collection []T) (T, int) {
// 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.
func MaxBy[T any](collection []T, comparison func(a, b T) bool) T {
//
// Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention.
// See https://github.com/samber/lo/issues/129
//
// Play: https://go.dev/play/p/JW1qu-ECwF7
func MaxBy[T any](collection []T, greater func(a, b T) bool) T {
var mAx T

if len(collection) == 0 {
Expand All @@ -468,7 +473,7 @@ func MaxBy[T any](collection []T, comparison func(a, b T) bool) T {
for i := 1; i < len(collection); i++ {
item := collection[i]

if comparison(item, mAx) {
if greater(item, mAx) {
mAx = item
}
}
Expand All @@ -479,7 +484,12 @@ func MaxBy[T any](collection []T, comparison func(a, b T) bool) T {
// 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, b T) bool) (T, int) {
//
// Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention.
// See https://github.com/samber/lo/issues/129
//
// Play: https://go.dev/play/p/uaUszc-c9QK
func MaxIndexBy[T any](collection []T, greater func(a, b T) bool) (T, int) {
var (
mAx T
index int
Expand All @@ -494,7 +504,7 @@ func MaxIndexBy[T any](collection []T, comparison func(a, b T) bool) (T, int) {
for i := 1; i < len(collection); i++ {
item := collection[i]

if comparison(item, mAx) {
if greater(item, mAx) {
mAx = item
index = i
}
Expand Down
Loading