github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/containers/enumerable.go (about)

     1  package containers
     2  
     3  // EnumerableWithIndex provides functions for ordered containers whose values can be fetched by an index.
     4  type EnumerableWithIndex[E any] interface {
     5  	// Each calls the given function once for each element, passing that element's index and value.
     6  	Each(func(index int, value E))
     7  
     8  	// Map invokes the given function once for each element and returns a
     9  	// container containing the values returned by the given function.
    10  	// Map(func(index int, value interface{}) interface{}) Container
    11  
    12  	// Select returns a new container containing all elements for which the given function returns a true value.
    13  	// Select(func(index int, value interface{}) bool) Container
    14  
    15  	// Any passes each element of the container to the given function and
    16  	// returns true if the function ever returns true for any element.
    17  	Any(func(index int, value E) bool) bool
    18  
    19  	// All passes each element of the container to the given function and
    20  	// returns true if the function returns true for all elements.
    21  	All(func(index int, value E) bool) bool
    22  
    23  	// Find passes each element of the container to the given function and returns
    24  	// the first (index,value) for which the function is true or -1,nil otherwise
    25  	// if no element matches the criteria.
    26  	Find(func(index int, value E) bool) (int, E)
    27  }
    28  
    29  // EnumerableWithKey provides functions for ordered containers whose values whose elements are key/value pairs.
    30  type EnumerableWithKey[K, V any] interface {
    31  	// Each calls the given function once for each element, passing that element's key and value.
    32  	Each(func(key K, value V))
    33  
    34  	// Map invokes the given function once for each element and returns a container
    35  	// containing the values returned by the given function as key/value pairs.
    36  	// Map(func(key interface{}, value interface{}) (interface{}, interface{})) Container
    37  
    38  	// Select returns a new container containing all elements for which the given function returns a true value.
    39  	// Select(func(key interface{}, value interface{}) bool) Container
    40  
    41  	// Any passes each element of the container to the given function and
    42  	// returns true if the function ever returns true for any element.
    43  	Any(func(key K, value V) bool) bool
    44  
    45  	// All passes each element of the container to the given function and
    46  	// returns true if the function returns true for all elements.
    47  	All(func(key K, value V) bool) bool
    48  
    49  	// Find passes each element of the container to the given function and returns
    50  	// the first (key,value) for which the function is true or nil,nil otherwise if no element
    51  	// matches the criteria.
    52  	Find(func(key K, value V) bool) (K, V)
    53  }