KeyBy converts a slice into a map using the key/value tuples returned by tupleFunc. If any two pairs would have the same key, the last one wins. Go maps are unordered and the order of the new map isn't guaranteed to the same as the original slice.
(collection []T, tupleFunc func(item T) (K, V))
| 34 | // maps are unordered and the order of the new map isn't guaranteed to the same |
| 35 | // as the original slice. |
| 36 | func KeyBy[T any, K comparable, V any](collection []T, tupleFunc func(item T) (K, V)) map[K]V { |
| 37 | result := make(map[K]V, len(collection)) |
| 38 | |
| 39 | for _, t := range collection { |
| 40 | k, v := tupleFunc(t) |
| 41 | result[k] = v |
| 42 | } |
| 43 | |
| 44 | return result |
| 45 | } |
| 46 | |
| 47 | // Map manipulates a slice and transforms it to a slice of another type. |
| 48 | func Map[T any, R any](collection []T, mapFunc func(T) R) []R { |
no outgoing calls
searching dependent graphs…