pair

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 0 Imported by: 0

README

pair

Combine parallel slices without index math.

Slices must be equal length — these are parallel data, not ragged collections. X[V1, V2] holds two values, accessed via .V1 and .V2.

pairs := pair.Zip(names, scores)  // []X[string, int]

What It Looks Like

// Zip and iterate
for _, p := range pair.Zip(names, ages) {
    fmt.Printf("%s is %d\n", p.V1, p.V2)
}
// Transform while zipping
users := pair.ZipWith(names, ages, NewUser)

Operations

  • Of[V1, V2](V1, V2) X[V1, V2] — create a pair
  • Zip[V1, V2]([]V1, []V2) []X[V1, V2] — combine slices into pairs
  • ZipWith[A, B, R]([]A, []B, func(A, B) R) []R — combine and transform

Zip and ZipWith panic if slice lengths differ.

See pkg.go.dev for complete API documentation, the main README for installation, and slice.Unzip for the inverse operation.

Documentation

Overview

Package pair provides tuple types and functions for working with pairs of values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ZipWith added in v0.6.0

func ZipWith[A, B, R any](as []A, bs []B, fn func(A, B) R) []R

ZipWith applies fn to corresponding elements of the two input slices. Panics if the slices have different lengths.

Types

type X

type X[V1, V2 any] struct {
	V1 V1
	V2 V2
}

X is a value of the Cartesian cross-product of the given types. Cross-product is represented by an x.

func Of

func Of[V, V2 any](v V, v2 V2) X[V, V2]

func Zip

func Zip[V1, V2 any](v1s []V1, v2s []V2) []X[V1, V2]

Zip returns a slice of each pair of elements from the two input slices. Panics if the slices have different lengths.

Jump to

Keyboard shortcuts

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