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

     1  // Package last provides helpers for retrieving a last element of a slice that satisfies a condition
     2  package last
     3  
     4  import (
     5  	"github.com/m4gshm/gollections/slice/last"
     6  )
     7  
     8  // Of an alias of the slice.Last
     9  func Of[T any](elements ...T) OfElements[T] {
    10  	return OfElements[T]{elements: elements}
    11  }
    12  
    13  // By an alias of the slice.Last
    14  func By[T any](by func(T) bool) ByPredicate[T] {
    15  	return ByPredicate[T]{predicate: by}
    16  }
    17  
    18  // ByPredicate tail of the By method
    19  type ByPredicate[T any] struct {
    20  	predicate func(T) bool
    21  }
    22  
    23  // Of the predicate apply method
    24  func (l ByPredicate[T]) Of(elements ...T) (T, bool) {
    25  	return last.Of(elements, l.predicate)
    26  }
    27  
    28  // OfElements tail of the Of method
    29  type OfElements[T any] struct {
    30  	elements []T
    31  }
    32  
    33  // By the predicate apply method
    34  func (l OfElements[T]) By(predicate func(T) bool) (T, bool) {
    35  	return last.Of(l.elements, predicate)
    36  }