]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmp/cmp.go
cmp: add Or
[gostls13.git] / src / cmp / cmp.go
index 0fba5c121162237c080c8aeb1b387fa8142f5330..4d1af6a98c4e46ee3f839196bf7a463d0cc6ce48 100644 (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
+}