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

     1  // Package convert provides converation helpers for collection implementations
     2  package convert
     3  
     4  import (
     5  	"github.com/m4gshm/gollections/collection"
     6  	"github.com/m4gshm/gollections/loop"
     7  	"github.com/m4gshm/gollections/op/check/not"
     8  )
     9  
    10  // AndConvert - convert.AndConvert makes double converts From->Intermediate->To of the elements
    11  func AndConvert[From, To, Too any, IT collection.Iterable[From]](elements IT, firsConverter func(From) To, secondConverter func(To) Too) loop.Loop[Too] {
    12  	cc := loop.Convert(collection.Convert(elements, firsConverter), secondConverter)
    13  	return cc
    14  }
    15  
    16  // AndFilter - convert.AndFilter converts only filtered elements and returns them
    17  func AndFilter[From, To any, IT collection.Iterable[From]](elements IT, converter func(From) To, filter func(To) bool) loop.Loop[To] {
    18  	return loop.Filter(collection.Convert(elements, converter), filter)
    19  }
    20  
    21  // NotNil - convert.NotNil converts only not nil elements and returns them
    22  func NotNil[From, To any, IT collection.Iterable[*From]](elements IT, converter func(*From) To) loop.Loop[To] {
    23  	return collection.FilterAndConvert(elements, not.Nil[From], converter)
    24  }