pairs

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PairsToMap

func PairsToMap[K comparable, V any](pairs []Pair[K, V]) map[K]V

PairsToMap converts a []Pair[K, V] to a map[K]V. In case of duplicate keys, the last value will be used.

func Sort

func Sort[K comparable, V any](pairs []Pair[K, V])

Sort sorts a list of Pairs alphabetically by their keys. The sort happens in-place. Note that this uses the Compare method, which may panic if the keys are not comparable.

func SortStable

func SortStable[K comparable, V any](pairs []Pair[K, V])

SortStable sorts a list of Pairs alphabetically by their keys. The sort happens in-place and is stable. Note that this uses the Compare method, which may panic if the keys are not comparable.

Types

type Comparable

type Comparable[T any] interface {
	// CompareTo returns -1 if receiver < b, 0 if receiver == b, and 1 if receiver > b.
	CompareTo(other T) int
}

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

Pair is a generic key-value pair.

func MapToPairs

func MapToPairs[K comparable, V any](pairs map[K]V) []Pair[K, V]

MapToPairs converts a map[K]V to a []Pair[K, V]. Note that the order of of the list is arbitrary.

func New

func New[K comparable, V any](key K, value V) Pair[K, V]

New creates a new Pair with the given key and value.

func (Pair[K, V]) CompareTo

func (p Pair[K, V]) CompareTo(other Pair[K, V]) int

Compare returns -1 if p.Key < other.Key, 0 if p.Key == other.Key, and 1 if p.Key > other.Key. It will panic if K does not implement the Comparable interface and cannot be parsed to an int64, float64, or string.

func (Pair[K, V]) String

func (p Pair[K, V]) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL