cmp: add Or

Fixes #60204

Change-Id: I1234cacf0f25097d034038bcfb33f6630373a057
GitHub-Last-Rev: e9098ed8b3
GitHub-Pull-Request: golang/go#60931
Reviewed-on: https://go-review.googlesource.com/c/go/+/504883
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
This commit is contained in:
Carl Johnson 2023-09-22 21:03:27 +00:00 committed by Gopher Robot
parent 81c5d92f52
commit 80e642cb7a
3 changed files with 85 additions and 0 deletions

1
api/next/60204.txt Normal file
View File

@ -0,0 +1 @@
pkg cmp, func Or[$0 comparable](...$0) $0 #60204

View File

@ -57,3 +57,15 @@ func Compare[T Ordered](x, y T) int {
func isNaN[T Ordered](x T) bool {
return x != x
}
// Or returns the first of its arguments that is not equal to the zero value.
// If no argument is non-zero, it returns the zero value.
func Or[T comparable](vals ...T) T {
var zero T
for _, val := range vals {
if val != zero {
return val
}
}
return zero
}

View File

@ -6,8 +6,10 @@ package cmp_test
import (
"cmp"
"fmt"
"math"
"sort"
"slices"
"testing"
)
@ -93,3 +95,73 @@ func TestSort(t *testing.T) {
}
}
}
func TestOr(t *testing.T) {
cases := []struct {
in []int
want int
}{
{nil, 0},
{[]int{0}, 0},
{[]int{1}, 1},
{[]int{0, 2}, 2},
{[]int{3, 0}, 3},
{[]int{4, 5}, 4},
{[]int{0, 6, 7}, 6},
}
for _, tc := range cases {
if got := cmp.Or(tc.in...); got != tc.want {
t.Errorf("cmp.Or(%v) = %v; want %v", tc.in, got, tc.want)
}
}
}
func ExampleOr() {
// Suppose we have some user input
// that may or may not be an empty string
userInput1 := ""
userInput2 := "some text"
fmt.Println(cmp.Or(userInput1, "default"))
fmt.Println(cmp.Or(userInput2, "default"))
fmt.Println(cmp.Or(userInput1, userInput2, "default"))
// Output:
// default
// some text
// some text
}
func ExampleOr_sort() {
type Order struct {
Product string
Customer string
Price float64
}
orders := []Order{
{"foo", "alice", 1.00},
{"bar", "bob", 3.00},
{"baz", "carol", 4.00},
{"foo", "alice", 2.00},
{"bar", "carol", 1.00},
{"foo", "bob", 4.00},
}
// Sort by customer first, product second, and last by higher price
slices.SortFunc(orders, func(a, b Order) int {
return cmp.Or(
cmp.Compare(a.Customer, b.Customer),
cmp.Compare(a.Product, b.Product),
cmp.Compare(b.Price, a.Price),
)
})
for _, order := range orders {
fmt.Printf("%s %s %.2f\n", order.Product, order.Customer, order.Price)
}
// Output:
// foo alice 2.00
// foo alice 1.00
// bar bob 3.00
// foo bob 4.00
// bar carol 1.00
// baz carol 4.00
}