github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/break/loop/loop.go (about)

     1  package loop
     2  
     3  // Loop is a function that returns the next element, false if there are no more elements or error if something is wrong.
     4  type Loop[T any] func() (T, bool, error)
     5  
     6  // All is used to iterate through the loop using `for ... range`. Supported since go 1.22 with GOEXPERIMENT=rangefunc enabled.
     7  func (next Loop[T]) All(consumer func(T, error) bool) {
     8  	All(next, consumer)
     9  }
    10  
    11  // For applies the 'consumer' function for the elements retrieved by the 'next' function until the consumer returns the c.Break to stop.
    12  func (next Loop[T]) For(consumer func(T) error) error {
    13  	return For(next, consumer)
    14  }
    15  
    16  // First returns the first element that satisfies the condition of the 'predicate' function
    17  func (next Loop[T]) First(predicate func(T) bool) (T, bool, error) {
    18  	return First(next, predicate)
    19  }
    20  
    21  // Slice collects the elements retrieved by the 'next' function into a new slice
    22  func (next Loop[T]) Slice() ([]T, error) {
    23  	return Slice(next)
    24  }
    25  
    26  // SliceCap collects the elements retrieved by the 'next' function into a new slice with predefined capacity
    27  func (next Loop[T]) SliceCap(cap int) ([]T, error) {
    28  	return SliceCap(next, cap)
    29  }
    30  
    31  // Append collects the elements retrieved by the 'next' function into the specified 'out' slice
    32  func (next Loop[T]) Append(out []T) ([]T, error) {
    33  	return Append(next, out)
    34  }
    35  
    36  // Reduce reduces the elements retrieved by the 'next' function into an one using the 'merge' function.
    37  func (next Loop[T]) Reduce(merge func(T, T) T) (T, error) {
    38  	return Reduce(next, merge)
    39  }
    40  
    41  // Reducee reduces the elements retrieved by the 'next' function into an one using the 'merge' function.
    42  func (next Loop[T]) Reducee(merge func(T, T) (T, error)) (T, error) {
    43  	return Reducee(next, merge)
    44  }
    45  
    46  // HasAny finds the first element that satisfies the 'predicate' function condition and returns true if successful
    47  func (next Loop[T]) HasAny(predicate func(T) bool) (bool, error) {
    48  	return HasAny(next, predicate)
    49  }
    50  
    51  // Filter creates a loop that checks elements by the 'filter' function and returns successful ones.
    52  func (next Loop[T]) Filter(filter func(T) bool) Loop[T] {
    53  	return Filter(next, filter)
    54  }
    55  
    56  // Crank rertieves a next element from the 'next' function, returns the function, element, successfully flag.
    57  func (next Loop[T]) Crank() (Loop[T], T, bool, error) {
    58  	return Crank(next)
    59  }