Skip to content

Commit

Permalink
cmp: add Or
Browse files Browse the repository at this point in the history
Fixes #60204

Change-Id: I1234cacf0f25097d034038bcfb33f6630373a057
GitHub-Last-Rev: e9098ed8b3dd9125661e4340ffe01d846670ba0f
GitHub-Pull-Request: golang/go#60931
Reviewed-on: https://go-review.googlesource.com/c/go/+/504883
Auto-Submit: Ian Lance Taylor <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Reviewed-by: qiulaidongfeng <[email protected]>
  • Loading branch information
earthboundkid authored and gopherbot committed Sep 25, 2023
1 parent 81c5d92 commit 80e642c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/next/60204.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg cmp, func Or[$0 comparable](...$0) $0 #60204
12 changes: 12 additions & 0 deletions src/cmp/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
72 changes: 72 additions & 0 deletions src/cmp/cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package cmp_test

import (
"cmp"
"fmt"
"math"
"sort"
"slices"
"testing"
)

Expand Down Expand Up @@ -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
}

0 comments on commit 80e642c

Please sign in to comment.