github.com/searKing/golang/go@v1.2.74/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  // OrFunc tests the slice satisfying f(c), or false if none do.
     8  // return true if len(s) == 0
     9  func OrFunc[S ~[]E, E any](s S, f func(E) bool) bool {
    10  	if len(s) == 0 {
    11  		return true
    12  	}
    13  	for _, e := range s {
    14  		if f(e) {
    15  			return true
    16  		}
    17  	}
    18  	return false
    19  }
    20  
    21  // AndFunc tests the slice satisfying f(c), or true if none do.
    22  // return true if len(s) == 0
    23  func AndFunc[S ~[]E, E any](s S, f func(E) bool) bool {
    24  	for _, e := range s {
    25  		if !f(e) {
    26  			return false
    27  		}
    28  	}
    29  	return true
    30  }
    31  
    32  // Or tests whether the slice satisfying c != zero within any c in the slice.
    33  // return true if len(s) == 0
    34  func Or[E comparable](s ...E) bool {
    35  	if len(s) == 0 {
    36  		return true
    37  	}
    38  	var zeroE E
    39  	for _, e := range s {
    40  		if e != zeroE {
    41  			return true
    42  		}
    43  	}
    44  	return false
    45  }
    46  
    47  // And tests whether the slice satisfying c != zero within all c in the slice.
    48  // return true if len(s) == 0
    49  func And[E comparable](s ...E) bool {
    50  	var zeroE E
    51  	for _, e := range s {
    52  		if e == zeroE {
    53  			return false
    54  		}
    55  	}
    56  	return true
    57  }