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

     1  // Package first provides short aliases for slice functions for retrieving a first element
     2  package first
     3  
     4  import (
     5  	"github.com/m4gshm/gollections/slice"
     6  )
     7  
     8  // Of returns the first element that satisfies requirements of the specified filter
     9  func Of[TS ~[]T, T any](elements TS, filter func(T) bool) (T, bool) {
    10  	return slice.First(elements, filter)
    11  }
    12  
    13  // Converted converts the first element that satisfies the condition of the 'filter' function by the converter and returns it
    14  func Converted[FS ~[]From, From, To any](elements FS, filter func(From) bool, converter func(From) To) (out To, ok bool) {
    15  	if f, ok := slice.First(elements, filter); ok {
    16  		return converter(f), true
    17  	}
    18  	return out, false
    19  }