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

     1  // Package resolv provides values resolvers for maps that builded by ToMap-converter functions
     2  package resolv
     3  
     4  import (
     5  	"cmp"
     6  	"slices"
     7  
     8  	"github.com/m4gshm/gollections/op"
     9  )
    10  
    11  // First keeps the first value of a key
    12  func First[K, V any](exists bool, _ K, old, new V) V { return op.IfElse(exists, old, new) }
    13  
    14  // Last retrieves the last value of a key
    15  func Last[K, V any](_ bool, _ K, _, new V) V { return new }
    16  
    17  // Slice puts the values of one key into a slice
    18  func Slice[K, V any](_ bool, _ K, rv []V, v V) []V {
    19  	return append(rv, v)
    20  }
    21  
    22  // SortedSlice puts the values of one key into a sorted slice
    23  func SortedSlice[K, V cmp.Ordered](_ bool, _ K, rv []V, v V) []V {
    24  	i, _ := slices.BinarySearch[[]V, V](rv, v)
    25  	r := append(append(rv[:i], v), rv[i:]...)
    26  	return r
    27  }