gitee.com/quant1x/gox@v1.21.2/util/internal/enumerable.go (about)

     1  // Copyright (c) 2015, Emir Pasic. 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 internal
     6  
     7  // EnumerableWithIndex provides functions for ordered containers whose values can be fetched by an index.
     8  type EnumerableWithIndex interface {
     9  	// Each calls the given function once for each element, passing that element's index and value.
    10  	Each(func(index int, value interface{}))
    11  
    12  	// Map invokes the given function once for each element and returns a
    13  	// container containing the values returned by the given function.
    14  	// TODO would appreciate help on how to enforce this in containers (don't want to type assert when chaining)
    15  	// Map(func(index int, value interface{}) interface{}) Container
    16  
    17  	// Select returns a new container containing all elements for which the given function returns a true value.
    18  	// TODO need help on how to enforce this in containers (don't want to type assert when chaining)
    19  	// Select(func(index int, value interface{}) bool) Container
    20  
    21  	// Any passes each element of the container to the given function and
    22  	// returns true if the function ever returns true for any element.
    23  	Any(func(index int, value interface{}) bool) bool
    24  
    25  	// All passes each element of the container to the given function and
    26  	// returns true if the function returns true for all elements.
    27  	All(func(index int, value interface{}) bool) bool
    28  
    29  	// Find passes each element of the container to the given function and returns
    30  	// the first (index,value) for which the function is true or -1,nil otherwise
    31  	// if no element matches the criteria.
    32  	Find(func(index int, value interface{}) bool) (int, interface{})
    33  }
    34  
    35  // EnumerableWithKey provides functions for ordered containers whose values whose elements are key/value pairs.
    36  type EnumerableWithKey interface {
    37  	// Each calls the given function once for each element, passing that element's key and value.
    38  	Each(func(key interface{}, value interface{}))
    39  
    40  	// Map invokes the given function once for each element and returns a container
    41  	// containing the values returned by the given function as key/value pairs.
    42  	// TODO need help on how to enforce this in containers (don't want to type assert when chaining)
    43  	// Map(func(key interface{}, value interface{}) (interface{}, interface{})) Container
    44  
    45  	// Select returns a new container containing all elements for which the given function returns a true value.
    46  	// TODO need help on how to enforce this in containers (don't want to type assert when chaining)
    47  	// Select(func(key interface{}, value interface{}) bool) Container
    48  
    49  	// Any passes each element of the container to the given function and
    50  	// returns true if the function ever returns true for any element.
    51  	Any(func(key interface{}, value interface{}) bool) bool
    52  
    53  	// All passes each element of the container to the given function and
    54  	// returns true if the function returns true for all elements.
    55  	All(func(key interface{}, value interface{}) bool) bool
    56  
    57  	// Find passes each element of the container to the given function and returns
    58  	// the first (key,value) for which the function is true or nil,nil otherwise if no element
    59  	// matches the criteria.
    60  	Find(func(key interface{}, value interface{}) bool) (interface{}, interface{})
    61  }