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

     1  // Package convert provides slice converation helpers
     2  package convert
     3  
     4  import (
     5  	"github.com/m4gshm/gollections/op/check/not"
     6  	"github.com/m4gshm/gollections/slice"
     7  )
     8  
     9  // AndConvert - convert.AndConvert makes double converts From->Intermediate->To of the elements
    10  func AndConvert[FS ~[]From, From, I, To any](elements FS, firsConverter func(From) I, secondConverter func(I) To) []To {
    11  	return slice.Convert(elements, func(from From) To { return secondConverter(firsConverter(from)) })
    12  }
    13  
    14  // AndFilter - convert.AndFilter converts only filtered elements and returns them
    15  func AndFilter[FS ~[]From, From, To any](elements FS, converter func(From) To, filter func(To) bool) []To {
    16  	return slice.ConvertAndFilter(elements, converter, filter)
    17  }
    18  
    19  // NotNil - convert.NotNil converts only not nil elements and returns them
    20  func NotNil[FS ~[]*From, From, To any](elements FS, converter func(*From) To) []To {
    21  	return slice.FilterAndConvert(elements, not.Nil[From], converter)
    22  }
    23  
    24  // ToNotNil - convert.ToNotNil converts elements and returns only not nil converted elements
    25  func ToNotNil[FS ~[]From, From, To any](elements FS, converter func(From) *To) []*To {
    26  	return slice.ConvertCheck(elements, func(f From) (*To, bool) {
    27  		if t := converter(f); t != nil {
    28  			return t, true
    29  		}
    30  		return nil, false
    31  	})
    32  }
    33  
    34  // NilSafe - convert.NilSafe filters not nil elements, converts that ones, filters not nils after converting and returns them
    35  func NilSafe[FS ~[]*From, From, To any](elements FS, converter func(*From) *To) []*To {
    36  	return slice.ConvertCheck(elements, func(f *From) (*To, bool) {
    37  		if f != nil {
    38  			if t := converter(f); t != nil {
    39  				return t, true
    40  			}
    41  		}
    42  		return nil, false
    43  	})
    44  }
    45  
    46  // Check - convert.Check is a short alias of slice.ConvertCheck
    47  func Check[FS ~[]From, From, To any](elements FS, converter func(from From) (To, bool)) []To {
    48  	return slice.ConvertCheck(elements, converter)
    49  }
    50  
    51  // CheckIndexed - convert.CheckIndexed is a short alias of slice.ConvertCheckIndexed
    52  func CheckIndexed[FS ~[]From, From, To any](elements FS, by func(index int, from From) (To, bool)) []To {
    53  	return slice.ConvertCheckIndexed(elements, by)
    54  }
    55  
    56  // Indexed - convert.Indexed is a short alias of slice.ConvertIndexed
    57  func Indexed[FS ~[]From, From, To any](elements FS, by func(index int, from From) To) []To {
    58  	return slice.ConvertIndexed(elements, by)
    59  }
    60  
    61  // AndReduce - convert.AndReduce converts elements and merge them into one
    62  func AndReduce[FS ~[]From, From, To any](elements FS, converter func(From) To, merge func(To, To) To) (out To) {
    63  	return slice.ConvertAndReduce(elements, converter, merge)
    64  }