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

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