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