Documentation
¶
Overview ¶
Package pair provides a generic two-element tuple and strict zip operations.
Pair is a value type holding First and Second. Construct via Of. Zip and ZipWith combine two equal-length slices element-wise, panicking on length mismatch rather than silently truncating.
Index ¶
Examples ¶
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 or fn is nil.
Example ¶
package main
import (
"fmt"
"github.com/binaryphile/fluentfp/tuple/pair"
)
func main() {
// Combine two slices with a function.
names := []string{"alice", "bob"}
ages := []int{30, 25}
// label combines name and age into a formatted string.
label := func(name string, age int) string {
return fmt.Sprintf("%s(%d)", name, age)
}
labels := pair.ZipWith(names, ages, label)
fmt.Println(labels)
}
Output: [alice(30) bob(25)]
Types ¶
type Pair ¶ added in v0.30.0
type Pair[A, B any] struct { First A Second B }
Pair holds two values of arbitrary types.
func Of ¶
Example ¶
package main
import (
"fmt"
"github.com/binaryphile/fluentfp/tuple/pair"
)
func main() {
// Construct a pair of name and age.
p := pair.Of("alice", 30)
fmt.Printf("%s is %d\n", p.First, p.Second)
}
Output: alice is 30
func Zip ¶
Zip returns a slice of each pair of elements from the two input slices. Panics if the slices have different lengths.
Example ¶
package main
import (
"fmt"
"github.com/binaryphile/fluentfp/tuple/pair"
)
func main() {
// Pair corresponding elements from two slices.
names := []string{"alice", "bob", "carol"}
ages := []int{30, 25, 35}
pairs := pair.Zip(names, ages)
for _, p := range pairs {
fmt.Printf("%s: %d\n", p.First, p.Second)
}
}
Output: alice: 30 bob: 25 carol: 35
Click to show internal directories.
Click to hide internal directories.