github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/filter.go (about)

     1  package isc
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  //ListFilter filter specificated item in a list
     8  func ListFilter[T any](list []T, f func(T) bool) []T {
     9  	var dest []T
    10  	return ListFilterTo(list, &dest, f)
    11  }
    12  
    13  //ListFilterNot Returns a list containing all elements not matching the given predicate.
    14  func ListFilterNot[T any](list []T, predicate func(T) bool) []T {
    15  	var n []T
    16  	return ListFilterNotTo(list, &n, predicate)
    17  }
    18  
    19  //ListFilterIndexed Returns a list containing only elements matching the given predicate.
    20  //Params: predicate - function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
    21  func ListFilterIndexed[T any](list []T, predicate func(int, T) bool) []T {
    22  	var n []T
    23  	return ListFilterIndexedTo(list, &n, predicate)
    24  }
    25  
    26  //ListFilterNotIndexed Appends all elements matching the given predicate to the given destination.
    27  //Params: predicate - function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
    28  func ListFilterNotIndexed[T any](list []T, f func(int, T) bool) []T {
    29  	var n []T
    30  	for i, e := range list {
    31  		if !f(i, e) {
    32  			n = append(n, e)
    33  		}
    34  	}
    35  	return n
    36  }
    37  
    38  //ListFilterNotNull Returns a list containing all elements that are not null.
    39  func ListFilterNotNull[T any](list []*T) []*T {
    40  	var n []*T
    41  	for _, e := range list {
    42  		if e != nil {
    43  			n = append(n, e)
    44  		}
    45  	}
    46  	return n
    47  }
    48  
    49  //ListFilterTo Appends all elements matching the given predicate to the given dest.
    50  func ListFilterTo[T any](list []T, dest *[]T, f func(T) bool) []T {
    51  	var n []T
    52  	for _, e := range list {
    53  		if f(e) {
    54  			*dest = append(*dest, e)
    55  			n = append(n, e)
    56  		}
    57  	}
    58  	return n
    59  }
    60  
    61  //ListFilterNotTo Appends all elements not matching the given predicate to the given destination.
    62  func ListFilterNotTo[T any](list []T, dest *[]T, predicate func(T) bool) []T {
    63  	var n []T
    64  	for _, e := range list {
    65  		if !predicate(e) {
    66  			*dest = append(*dest, e)
    67  			n = append(n, e)
    68  		}
    69  	}
    70  	return n
    71  }
    72  
    73  //ListFilterIndexedTo Appends all elements matching the given predicate to the given destination.
    74  //Params: predicate - function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
    75  func ListFilterIndexedTo[T any](list []T, dest *[]T, predicate func(int, T) bool) []T {
    76  	var n []T
    77  	for i, e := range list {
    78  		if predicate(i, e) {
    79  			*dest = append(*dest, e)
    80  			n = append(n, e)
    81  		}
    82  	}
    83  	return n
    84  }
    85  
    86  //ListFilterNotIndexedTo Appends all elements not matching the given predicate to the given destination.
    87  //Params: predicate - function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
    88  func ListFilterNotIndexedTo[T any](list []T, dest *[]T, predicate func(int, T) bool) []T {
    89  	var n []T
    90  	for i, e := range list {
    91  		if !predicate(i, e) {
    92  			*dest = append(*dest, e)
    93  			n = append(n, e)
    94  		}
    95  	}
    96  	return n
    97  }
    98  
    99  func ListContains[T any](list []T, item T) bool {
   100  	ret := false
   101  	for _, e := range list {
   102  		if reflect.DeepEqual(e, item) {
   103  			ret = true
   104  			break
   105  		}
   106  	}
   107  	return ret
   108  }
   109  
   110  func ListDistinct[T any](list []T) []T {
   111  	return SliceDistinct(list)
   112  }
   113  
   114  /// functions for map
   115  
   116  func MapFilter[K comparable, V any](m map[K]V, f func(K, V) bool) map[K]V {
   117  	n := make(map[K]V)
   118  	for k, v := range m {
   119  		if f(k, v) {
   120  			n[k] = v
   121  		}
   122  	}
   123  	return n
   124  }
   125  
   126  func MapFilterNot[K comparable, V any](m map[K]V, f func(K, V) bool) map[K]V {
   127  	n := make(map[K]V)
   128  	for k, v := range m {
   129  		if !f(k, v) {
   130  			n[k] = v
   131  		}
   132  	}
   133  	return n
   134  }
   135  
   136  func MapFilterKeys[K comparable, V any](m map[K]V, f func(K) bool) map[K]V {
   137  	n := make(map[K]V)
   138  	for k, v := range m {
   139  		if f(k) {
   140  			n[k] = v
   141  		}
   142  	}
   143  	return n
   144  }
   145  
   146  func MapFilterValues[K comparable, V any](m map[K]V, f func(V) bool) map[K]V {
   147  	n := make(map[K]V)
   148  	for k, v := range m {
   149  		if f(v) {
   150  			n[k] = v
   151  		}
   152  	}
   153  	return n
   154  }
   155  
   156  func MapFilterTo[K comparable, V any](m map[K]V, dest *map[K]V, f func(K, V) bool) map[K]V {
   157  	n := make(map[K]V)
   158  	for k, v := range m {
   159  		if f(k, v) {
   160  			(*dest)[k] = v
   161  			n[k] = v
   162  		}
   163  	}
   164  	return n
   165  }
   166  
   167  func MapFilterNotTo[K comparable, V any](m map[K]V, dest *map[K]V, f func(K, V) bool) map[K]V {
   168  	n := make(map[K]V)
   169  	for k, v := range m {
   170  		if !f(k, v) {
   171  			(*dest)[k] = v
   172  			n[k] = v
   173  		}
   174  	}
   175  	return n
   176  }
   177  
   178  func MapContains[K comparable, V any](m map[K]V, k K, v V) bool {
   179  	ret := false
   180  	for t, u := range m {
   181  		if t == k && reflect.DeepEqual(u, v) {
   182  			ret = true
   183  			break
   184  		}
   185  	}
   186  	return ret
   187  }
   188  
   189  func MapContainsKey[K comparable, V any](m map[K]V, k K) bool {
   190  	ret := false
   191  	for t := range m {
   192  		if t == k {
   193  			ret = true
   194  			break
   195  		}
   196  	}
   197  	return ret
   198  }
   199  
   200  func MapContainsValue[K comparable, V any](m map[K]V, v V) bool {
   201  	ret := false
   202  	for _, u := range m {
   203  		if reflect.DeepEqual(u, v) {
   204  			ret = true
   205  			break
   206  		}
   207  	}
   208  	return ret
   209  }