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

     1  // Package split provides utils for splitting slices
     2  package split
     3  
     4  import "github.com/m4gshm/gollections/slice"
     5  
     6  // Of splits the elements into two slices
     7  func Of[TS ~[]T, T, F, S any](elements TS, firstConverter func(T) F, secondConverter func(T) S) ([]F, []S) {
     8  	return slice.SplitTwo(elements, func(t T) (F, S) { return firstConverter(t), secondConverter(t) })
     9  }
    10  
    11  // AndReduce - split.AndReduce splits each element of the specified slice into two values and then reduces that ones
    12  func AndReduce[TS ~[]T, T, F, S any](elements TS, firstConverter func(T) F, secondConverter func(T) S, firstMerge func(F, F) F, secondMerger func(S, S) S) (F, S) {
    13  	return slice.SplitAndReduceTwo(elements, func(t T) (F, S) { return firstConverter(t), secondConverter(t) }, firstMerge, secondMerger)
    14  }