tuple

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2025 License: Apache-2.0 Imports: 0 Imported by: 0

README

tuple

Package tuple provides extremely simple generic tuple types and utility functions for creating tuples.

Go Reference Go Report Card

Usage Example

Here's an example of how you might use the tuple package for managing multiple return values from functions:

Handling Multiple Return Values

Consider a scenario where you have a function that returns two integers and another function that accepts a string and a pair of integers.

package main

import (
	"fmt"

	"github.com/goaux/tuple"
)

// GetXY is an example function returning a pair of values.
func GetXY() (x, y int) {
	return 12, 34
}

// PrintXY is an example function accepting a string and a pair of ints.
func PrintXY(tag string, x, y int) {
	fmt.Printf("%s: X=%d Y=%d\n", tag, x, y)
}

// PrintXYPair is an example function wraps PrintXY with accepting a string and a Pair[int, int].
func PrintXYPair(tag string, xy tuple.Pair[int, int]) {
	PrintXY(tag, xy.First, xy.Second)
}

func main() {
	// Cannot write PrintXY("here", GetXY()) directly.
	x, y := GetXY()
	PrintXY("here", x, y)

	// Using tuple eliminates the need for intermediate variables.
	PrintXYPair("here", tuple.MakePair(GetXY()))
}

Tuples Types

Pair[T0, T1]

Represents a pair of values.

  • First holds the first value.
  • Second holds the second value.
Triple[T0, T1, T2]

Represents a triple of values.

  • First, Second, and Third hold the respective values.
Quadruple[T0, T1, T2, T3]

Represents a quadruple of values.

  • First, Second, Third, and Fourth hold the respective values.
Quintuple[T0, T1, T2, T3, T4]

Represents a quintuple of values.

  • First, Second, Third, Fourth, and Fifth hold the respective values.

Utility Functions

Each tuple type includes utility functions:

  • MakePair, NewPair: Create new pairs.
  • Len() int: Returns the number of elements in the tuple.
  • Get(int) any: Gets an element at index i. Returns nil if out-of-bounds.

Similar methods exist for Triple, Quadruple, and Quintuple.

Tuple Interface

The Tuple interface defines two methods: Len() int and Get(int) any. Pair, Triple, Quadruple, and Quintuple all implement the Tuple interface.

Documentation

Overview

Package tuple provides extremely simple generic tuple types and utility functions for creating tuples.

Example
package main

import (
	"fmt"

	"github.com/goaux/tuple"
)

// GetXY is an example function returning a pair of values.
func GetXY() (x, y int) {
	return 12, 34
}

// PrintXY is an example function accepting a string and a pair of ints.
func PrintXY(tag string, x, y int) {
	fmt.Printf("%s: X=%d Y=%d\n", tag, x, y)
}

// PrintXYPair is an example function wraps PrintXY with accepting a string and a Pair[int, int].
func PrintXYPair(tag string, xy tuple.Pair[int, int]) {
	PrintXY(tag, xy.First, xy.Second)
}

func main() {
	// Cannot write PrintXY("here", GetXY()) directly.
	x, y := GetXY()
	PrintXY("here", x, y)

	// Using tuple eliminates the need for intermediate variables.
	PrintXYPair("here", tuple.MakePair(GetXY()))
}
Output:

here: X=12 Y=34
here: X=12 Y=34

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pair

type Pair[T0, T1 any] struct {
	First  T0
	Second T1
}

Pair is a generic type for a pair of values.

func MakePair

func MakePair[T0, T1 any](first T0, second T1) Pair[T0, T1]

MakePair returns a new Pair with the given values.

func NewPair

func NewPair[T0, T1 any](first T0, second T1) *Pair[T0, T1]

NewPair returns a pointer to a new Pair with the given values.

func (Pair[_, _]) Get

func (v Pair[_, _]) Get(i int) any

Get returns the value at index i. If the index is out of bounds, it returns nil.

func (Pair[_, _]) Len

func (Pair[_, _]) Len() int

Len returns 2.

func (Pair[T0, T1]) Unpack added in v1.1.0

func (v Pair[T0, T1]) Unpack() (T0, T1)

Unpack returns the elements of the pair as a tuple.

type Quadruple

type Quadruple[T0, T1, T2, T3 any] struct {
	First  T0
	Second T1
	Third  T2
	Fourth T3
}

Quadruple is a generic type for a quadruple of values.

func MakeQuadruple

func MakeQuadruple[T0, T1, T2, T3 any](first T0, second T1, third T2, fourth T3) Quadruple[T0, T1, T2, T3]

MakeQuadruple returns a new Quadruple with the given values.

func NewQuadruple

func NewQuadruple[T0, T1, T2, T3 any](first T0, second T1, third T2, fourth T3) *Quadruple[T0, T1, T2, T3]

NewQuadruple returns a pointer to a new Quadruple with the given values.

func (Quadruple[_, _, _, _]) Get

func (q Quadruple[_, _, _, _]) Get(i int) any

Get returns the value at index i. If the index is out of bounds, it returns nil.

func (Quadruple[_, _, _, _]) Len

func (q Quadruple[_, _, _, _]) Len() int

Len returns 4.

func (Quadruple[T0, T1, T2, T3]) Unpack added in v1.1.0

func (v Quadruple[T0, T1, T2, T3]) Unpack() (T0, T1, T2, T3)

Unpack returns the elements of the quadruple as a tuple.

type Quintuple

type Quintuple[T0, T1, T2, T3, T4 any] struct {
	First  T0
	Second T1
	Third  T2
	Fourth T3
	Fifth  T4
}

Quintuple is a generic type for a quintuple of values.

func MakeQuintuple

func MakeQuintuple[T0, T1, T2, T3, T4 any](first T0, second T1, third T2, fourth T3, fifth T4) Quintuple[T0, T1, T2, T3, T4]

MakeQuintuple returns a new Quintuple with the given values.

func NewQuintuple

func NewQuintuple[T0, T1, T2, T3, T4 any](first T0, second T1, third T2, fourth T3, fifth T4) *Quintuple[T0, T1, T2, T3, T4]

NewQuintuple returns a pointer to a new Quintuple with the given values.

func (Quintuple[_, _, _, _, _]) Get

func (q Quintuple[_, _, _, _, _]) Get(i int) any

Get returns the value at index i. If the index is out of bounds, it returns nil.

func (Quintuple[_, _, _, _, _]) Len

func (q Quintuple[_, _, _, _, _]) Len() int

Len returns 5.

func (Quintuple[T0, T1, T2, T3, T4]) Unpack added in v1.1.0

func (v Quintuple[T0, T1, T2, T3, T4]) Unpack() (T0, T1, T2, T3, T4)

Unpack returns the elements of the quintuple as a tuple.

type Triple

type Triple[T0, T1, T2 any] struct {
	First  T0
	Second T1
	Third  T2
}

Triple is a generic type for a triple of values.

func MakeTriple

func MakeTriple[T0, T1, T2 any](first T0, second T1, third T2) Triple[T0, T1, T2]

MakeTriple returns a new Triple with the given values.

func NewTriple

func NewTriple[T0, T1, T2 any](first T0, second T1, third T2) *Triple[T0, T1, T2]

NewTriple returns a pointer to a new Triple with the given values.

func (Triple[_, _, _]) Get

func (t Triple[_, _, _]) Get(i int) any

Get returns the value at index i. If the index is out of bounds, it returns nil.

func (Triple[_, _, _]) Len

func (t Triple[_, _, _]) Len() int

Len returns 3.

func (Triple[T0, T1, T2]) Unpack added in v1.1.0

func (v Triple[T0, T1, T2]) Unpack() (T0, T1, T2)

Unpack returns the elements of the triple as a tuple.

type Tuple

type Tuple interface {
	// Len returns the number of elements in the tuple.
	Len() int

	// Get returns an element at index i from the tuple.
	// If the index is out of bounds, it returns nil.
	Get(int) any
}

Tuple is a generic interface that represents a tuple of elements.

Jump to

Keyboard shortcuts

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