From 7ecfbd3107149502135ed148b6651e98b05e36d7 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 13 Feb 2023 12:24:48 +0800 Subject: [PATCH] =?UTF-8?q?#I6ENH7=20=E5=AE=9E=E7=8E=B0any=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stat/any.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ stat/any_test.go | 19 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 stat/any.go create mode 100644 stat/any_test.go diff --git a/stat/any.go b/stat/any.go new file mode 100644 index 0000000..bfb3061 --- /dev/null +++ b/stat/any.go @@ -0,0 +1,47 @@ +package stat + +import "github.com/viterin/vek" + +// Any 任意一个为真 +func Any[T Number | ~bool](x []T) bool { + switch vs := any(x).(type) { + case []bool: + return vek.Any(vs) + case []int8: + return __any_go(vs) + case []uint8: + return __any_go(vs) + case []int16: + return __any_go(vs) + case []uint16: + return __any_go(vs) + case []int32: + return __any_go(vs) + case []uint32: + return __any_go(vs) + case []int64: + return __any_go(vs) + case []uint64: + return __any_go(vs) + case []int: + return __any_go(vs) + case []uint: + return __any_go(vs) + case []uintptr: + return __any_go(vs) + case []float32: + return __any_go(vs) + case []float64: + return __any_go(vs) + } + return false +} + +func __any_go[T Number](x []T) bool { + for i := 0; i < len(x); i++ { + if x[i] != 0 { + return true + } + } + return false +} diff --git a/stat/any_test.go b/stat/any_test.go new file mode 100644 index 0000000..26487f0 --- /dev/null +++ b/stat/any_test.go @@ -0,0 +1,19 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestAny(t *testing.T) { + d1 := []bool{true, true} + d2 := []bool{true, false} + d3 := []uintptr{0, 1} + d4 := []int{1, 1} + d5 := []float64{1.0, 0} + fmt.Println(Any(d1)) + fmt.Println(Any(d2)) + fmt.Println(Any(d3)) + fmt.Println(Any(d4)) + fmt.Println(Any(d5)) +} -- Gitee