github.com/searKing/golang/go@v1.2.117/exp/slices/logic.go (about) 1 // Copyright 2022 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package slices 6 7 // https://en.wikipedia.org/wiki/Boolean_operation 8 9 // OrFunc tests the slice satisfying f(c), or false if none do. 10 // return true if len(s) == 0 11 func OrFunc[S ~[]E, E any](s S, f func(E) bool) bool { 12 if len(s) == 0 { 13 return true 14 } 15 for _, e := range s { 16 if f(e) { 17 return true 18 } 19 } 20 return false 21 } 22 23 // AndFunc tests the slice satisfying f(c), or true if none do. 24 // return true if len(s) == 0 25 func AndFunc[S ~[]E, E any](s S, f func(E) bool) bool { 26 for _, e := range s { 27 if !f(e) { 28 return false 29 } 30 } 31 return true 32 } 33 34 // Or tests whether the slice satisfying c != zero within any c in the slice. 35 // return true if len(s) == 0 36 func Or[E comparable](s ...E) bool { 37 if len(s) == 0 { 38 return true 39 } 40 var zeroE E 41 for _, e := range s { 42 if e != zeroE { 43 return true 44 } 45 } 46 return false 47 } 48 49 // And tests whether the slice satisfying c != zero within all c in the slice. 50 // return true if len(s) == 0 51 func And[E comparable](s ...E) bool { 52 var zeroE E 53 for _, e := range s { 54 if e == zeroE { 55 return false 56 } 57 } 58 return true 59 }