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

     1  package loop
     2  
     3  import (
     4  	"github.com/m4gshm/gollections/break/loop"
     5  	"github.com/m4gshm/gollections/c"
     6  )
     7  
     8  // Loop is a function that returns the next element or false if there are no more elements.
     9  type Loop[T any] func() (T, bool)
    10  
    11  var (
    12  	_ c.Filterable[any, Loop[any], loop.Loop[any]]  = (Loop[any])(nil)
    13  	_ c.Convertable[any, Loop[any], loop.Loop[any]] = (Loop[any])(nil)
    14  )
    15  
    16  // All is used to iterate through the loop using `for ... range`. Supported since go 1.22 with GOEXPERIMENT=rangefunc enabled.
    17  func (next Loop[T]) All(consumer func(T) bool) {
    18  	All(next, consumer)
    19  }
    20  
    21  // For applies the 'consumer' function for the elements retrieved by the 'next' function until the consumer returns the c.Break to stop.
    22  func (next Loop[T]) For(consumer func(T) error) error {
    23  	return For(next, consumer)
    24  }
    25  
    26  // ForEach applies the 'consumer' function to the elements retrieved by the 'next' function
    27  func (next Loop[T]) ForEach(consumer func(T)) {
    28  	ForEach(next, consumer)
    29  }
    30  
    31  // ForEachFiltered applies the 'consumer' function to the elements retrieved by the 'next' function that satisfy the 'predicate' function condition
    32  func (next Loop[T]) ForEachFiltered(predicate func(T) bool, consumer func(T)) {
    33  	ForEachFiltered(next, predicate, consumer)
    34  }
    35  
    36  // First returns the first element that satisfies the condition of the 'predicate' function
    37  func (next Loop[T]) First(predicate func(T) bool) (T, bool) {
    38  	return First(next, predicate)
    39  }
    40  
    41  // Slice collects the elements retrieved by the 'next' function into a new slice
    42  func (next Loop[T]) Slice() []T {
    43  	return Slice(next)
    44  }
    45  
    46  // SliceCap collects the elements retrieved by the 'next' function into a new slice with predefined capacity
    47  func (next Loop[T]) SliceCap(cap int) []T {
    48  	return SliceCap(next, cap)
    49  }
    50  
    51  // Append collects the elements retrieved by the 'next' function into the specified 'out' slice
    52  func (next Loop[T]) Append(out []T) []T {
    53  	return Append(next, out)
    54  }
    55  
    56  // Reduce reduces the elements retrieved by the 'next' function into an one using the 'merge' function.
    57  func (next Loop[T]) Reduce(merge func(T, T) T) T {
    58  	return Reduce(next, merge)
    59  }
    60  
    61  // Reducee reduces the elements retrieved by the 'next' function into an one using the 'merge' function
    62  func (next Loop[T]) Reducee(merge func(T, T) (T, error)) (T, error) {
    63  	return Reducee(next, merge)
    64  }
    65  
    66  // HasAny finds the first element that satisfies the 'predicate' function condition and returns true if successful
    67  func (next Loop[T]) HasAny(predicate func(T) bool) bool {
    68  	return HasAny(next, predicate)
    69  }
    70  
    71  // Filt creates a loop that checks elements by the 'filter' function and returns successful ones.
    72  func (next Loop[T]) Filt(filter func(T) (bool, error)) loop.Loop[T] {
    73  	return Filt(next, filter)
    74  }
    75  
    76  // Filter creates a loop that checks elements by the 'filter' function and returns successful ones.
    77  func (next Loop[T]) Filter(filter func(T) bool) Loop[T] {
    78  	return Filter(next, filter)
    79  }
    80  
    81  // Convert creates a loop that applies the 'converter' function to iterable elements.
    82  func (next Loop[T]) Convert(converter func(T) T) Loop[T] {
    83  	return Convert(next, converter)
    84  }
    85  
    86  // Conv creates a loop that applies the 'converter' function to iterable elements.
    87  func (next Loop[T]) Conv(converter func(T) (T, error)) loop.Loop[T] {
    88  	return Conv(next, converter)
    89  }
    90  
    91  // Crank rertieves a next element from the 'next' function, returns the function, element, successfully flag.
    92  func (next Loop[T]) Crank() (Loop[T], T, bool) {
    93  	return Crank(next)
    94  }