generics/pairs.go

29 lines
562 B
Go
Raw Normal View History

package generics
// Pair represents key-value pair.
type Pair[K comparable, V any] struct {
Key K
Value V
}
// Pairs represents a collection of key-value pairs.
type Pairs[K comparable, V any] []Pair[K, V]
// Keys returns list of key-value pairs' keys.
func (p Pairs[K, V]) Keys() []K {
dst := make([]K, len(p))
for i, pair := range p {
dst[i] = pair.Key
}
return dst
}
// Values returns list of key-value pairs' values.
func (p Pairs[K, V]) Values() []V {
dst := make([]V, len(p))
for i, pair := range p {
dst[i] = pair.Value
}
return dst
}