pair

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 0 Imported by: 0

README

pair: tuple type and zip functions

Combine two slices element-by-element. A pair holds two values of potentially different types.

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

See pkg.go.dev for complete API documentation.

Quick Start

import "github.com/binaryphile/fluentfp/tuple/pair"

// Zip two slices into pairs
pairs := pair.Zip(names, ages)
for _, p := range pairs {
    fmt.Printf("%s is %d\n", p.V1, p.V2)
}

// Transform while zipping
users := pair.ZipWith(names, ages, NewUser)

Types

X[V1, V2] holds two values. Access via .V1 and .V2 fields.

API Reference

Function Signature Purpose Example
Of Of[V1,V2](V1, V2) X[V1,V2] Create pair p = pair.Of("alice", 30)
Zip Zip[V1,V2]([]V1, []V2) []X[V1,V2] Combine slices pairs = pair.Zip(names, ages)
ZipWith ZipWith[A,B,R]([]A, []B, func(A,B)R) []R Combine and transform users = pair.ZipWith(names, ages, NewUser)

Both Zip and ZipWith panic if slices have different lengths.

When NOT to Use pair

  • More than two values — Use a named struct instead
  • Semantically meaningful fieldsUser{Name, Age} beats X[string, int]
  • Single slice iteration — Just use for range

See Also

For extracting multiple fields from a single slice, see slice.Unzip2/3/4.

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