github.com/oinume/lekcije@v0.0.0-20231017100347-5b4c5eb6ab24/backend/internal/slice_util/slice_util.go (about) 1 package slice_util 2 3 import ( 4 "sort" 5 6 "golang.org/x/exp/constraints" 7 ) 8 9 func Sort[T constraints.Ordered](s []T) { 10 sort.Slice(s, func(i, j int) bool { 11 return s[i] < s[j] 12 }) 13 } 14 15 // Map manipulates a slice and transforms it to a slice of another type. 16 // Play: https://go.dev/play/p/OkPcYAhBo0D 17 func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R { 18 result := make([]R, len(collection)) 19 20 for i, item := range collection { 21 result[i] = iteratee(item, i) 22 } 23 24 return result 25 }