pair

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package pair provides a simple Pair type to hold two values of potentially different types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare[T, U Ordered](a, b Pair[T, U]) int

Compare provides an integer representing the relative order of two Pair values. The semantics of the result are the same as for cmp.Compare, but the First fields are compared first, and only if they are equal are the Second fields compared.

This only works when both fields implement the Ordered interface.

func Equal

func Equal[T, U comparable](a, b Pair[T, U]) bool

Equal compares two Pair values for equality. It returns true if the two Pairs have the same First and Second values, and false otherwise.

This only works when both fields implement the comparable interface.

func Less

func Less[T, U Ordered](a, b Pair[T, U]) bool

Less compares two Pair values by comparing their fields using the First field as the most significant field, and falling through to comparing the Second field if the First fields are equal.

This only works when both fields implement the Ordered interface.

Types

type Ordered

type Ordered = cmp.Ordered

Ordered is an alias for the cmp.Ordered type, which represents any type that implements the standard comparison operators (<, >, <=, >=, ==, !=).

type Pair

type Pair[T, U any] struct {
	A T
	B U
}

A Pair holds two values of potentially different types.

func Of added in v0.0.5

func Of[T, U any](a T, b U) Pair[T, U]

Of creates a new Pair with the given values.

func (Pair[T, U]) First

func (p Pair[T, U]) First() T

First returns the first value stored in the Pair.

func (Pair[T, U]) Format

func (p Pair[T, U]) Format(f fmt.State, _ rune)

Format writes a string representation of the Pair in the format "Pair{<First>,<Second>}" as part of a fmt.*printf call. The format verb is ignored. If the # flag is set, the type parameters are also included in the format "Pair[T,U]{<First>,<Second>}".

func (Pair[T, U]) Second

func (p Pair[T, U]) Second() U

Second returns the second value stored in the Pair.

func (Pair[T, U]) String

func (p Pair[T, U]) String() string

String returns a string representation of the Pair in the format "Pair{<First>, <Second>}".

func (Pair[T, U]) Swap added in v0.0.5

func (p Pair[T, U]) Swap() Pair[U, T]

Swap returns a new Pair with the First and Second values swapped.

func (Pair[T, U]) Unpack added in v0.0.5

func (p Pair[T, U]) Unpack() (T, U)

Unpack returns the two values stored in the Pair.

Jump to

Keyboard shortcuts

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