From 0c5e7ceebd76a3b774b83e9a984c70f2a74bd64d Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Wed, 5 Nov 2025 19:32:27 +0100 Subject: [PATCH] doc(maxby): adding comment to explain inconsistency --- README.md | 4 ++++ docs/data/core-maxby.md | 4 +++- docs/data/core-maxindexby.md | 4 +++- find.go | 26 ++++++++++++++++++-------- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b5b069f2..7d96aeff 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/docs/data/core-maxby.md b/docs/data/core-maxby.md index 9663be1d..0b2e1f3d 100644 --- a/docs/data/core-maxby.md +++ b/docs/data/core-maxby.md @@ -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: @@ -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 diff --git a/docs/data/core-maxindexby.md b/docs/data/core-maxindexby.md index e356c1b7..22a5a240 100644 --- a/docs/data/core-maxindexby.md +++ b/docs/data/core-maxindexby.md @@ -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: @@ -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 diff --git a/find.go b/find.go index 534b4d75..bda7ec10 100644 --- a/find.go +++ b/find.go @@ -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 { @@ -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 } } @@ -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 @@ -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 } @@ -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 { @@ -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 } } @@ -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 @@ -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 }