Documentation
¶
Overview ¶
Package collections provides functional programming utilities for working with Go sequences and slices.
The package includes a generic Set type for mathematical set operations and efficient membership testing.
This package leverages Go's iter.Seq type to provide efficient, composable operations on both sequences and slices. Each operation comes in two variants: one for sequences (Seq suffix) and one for slices (Slice suffix).
Index ¶
- func ConcatSeq[T any](seqs ...iter.Seq[T]) iter.Seq[T]
- func FilterMapSeq[T, V any](seq iter.Seq[T], fn func(T) (V, bool)) iter.Seq[V]
- func FilterMapSlice[TSlice ~[]T, T, V any](s TSlice, fn func(T) (V, bool)) []V
- func FilterSeq[T any](seq iter.Seq[T], predicate func(T) bool) iter.Seq[T]
- func FilterSlice[TSlice ~[]T, T any](s TSlice, predicate func(T) bool) TSlice
- func FlatMapSeq[VSlice ~[]V, T, V any](seq iter.Seq[T], fn func(T) VSlice) iter.Seq[V]
- func FlatMapSlice[TSlice ~[]T, VSlice ~[]V, T, V any](s TSlice, fn func(T) VSlice) VSlice
- func MapSeq[T, V any](seq iter.Seq[T], fn func(T) V) iter.Seq[V]
- func MapSlice[TSlice ~[]T, T, V any](s TSlice, fn func(T) V) []V
- type Set
- func (s Set[T]) Add(elem T) Set[T]
- func (s Set[T]) AddSeq(elems iter.Seq[T]) Set[T]
- func (s Set[T]) AddSlice(elems []T) Set[T]
- func (s Set[T]) All() iter.Seq[T]
- func (s Set[T]) Contains(elem T) bool
- func (s Set[T]) Diff(other Set[T]) Set[T]
- func (s Set[T]) Intersect(other Set[T]) Set[T]
- func (s Set[T]) Intersects(other Set[T]) bool
- func (s Set[T]) Join(other Set[T]) Set[T]
- func (s Set[T]) SortedValues(cmp func(l, r T) int) []T
- func (s Set[T]) Values() []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConcatSeq ¶ added in v0.5.0
ConcatSeq concatenates multiple input sequences into a single sequence.
Example ¶
seq := ConcatSeq(
slices.Values([]string{"a", "b"}),
slices.Values([]string{"c", "d"}),
)
fmt.Println(slices.Collect(seq))
Output: [a b c d]
func FilterMapSeq ¶
FilterMapSeq applies a transformation function `fn` to each element of the input sequence `seq`, where `fn` returns both a transformed value and a boolean indicating success. Returns a new sequence containing only the successfully transformed values.
Example ¶
seq := FilterMapSeq(
slices.Values([]int{1, -1, 2}),
func(x int) (int, bool) {
if x < 0 {
return 0, false
}
return x * 2, true
},
)
fmt.Println(slices.Collect(seq))
Output: [2 4]
func FilterMapSlice ¶
FilterMapSlice applies a transformation function `fn` to each element of the input slice `s`, where `fn` returns both a transformed value and a boolean indicating success. Returns a new slice containing only the successfully transformed values.
Example ¶
result := FilterMapSlice(
[]int{1, -1, 2},
func(x int) (int, bool) {
if x < 0 {
return 0, false
}
return x * 2, true
},
)
fmt.Println(result)
Output: [2 4]
func FilterSeq ¶
FilterSeq returns a new sequence containing only the elements of `seq` for which the `predicate` function returns true.
Example ¶
seq := FilterSeq(
slices.Values([]int{1, 2, 3, 4}),
func(x int) bool { return x%2 == 0 },
)
fmt.Println(slices.Collect(seq))
Output: [2 4]
func FilterSlice ¶
FilterSlice returns a new slice containing only the elements of `s` for which the `predicate` function returns true.
Example ¶
result := FilterSlice([]int{1, 2, 3, 4}, func(x int) bool { return x%2 == 0 })
fmt.Println(result)
Output: [2 4]
func FlatMapSeq ¶
FlatMapSeq applies the provided transformation function `fn` to each element of the input sequence `seq`, where `fn` returns a slice, and flattens the resulting slices into a single sequence.
Example ¶
seq := FlatMapSeq(
slices.Values([]int{1, 2}),
func(x int) []int { return []int{x, x} },
)
fmt.Println(slices.Collect(seq))
Output: [1 1 2 2]
func FlatMapSlice ¶
func FlatMapSlice[TSlice ~[]T, VSlice ~[]V, T, V any](s TSlice, fn func(T) VSlice) VSlice
FlatMapSlice applies the provided transformation function `fn` to each element of the input slice `s`, where `fn` returns a slice, and flattens the resulting slices into a single slice.
Example ¶
result := FlatMapSlice(
[]int{1, 2},
func(x int) []int { return []int{x, x} },
)
fmt.Println(result)
Output: [1 1 2 2]
func MapSeq ¶
MapSeq applies the provided transformation function `fn` to each element of the input sequence `seq` and returns a new sequence of the resulting values.
Example ¶
seq := MapSeq(
slices.Values([]int{1, 2, 3}),
func(x int) string { return fmt.Sprint(x) },
)
fmt.Println(slices.Collect(seq))
Output: [1 2 3]
func MapSlice ¶
func MapSlice[TSlice ~[]T, T, V any](s TSlice, fn func(T) V) []V
MapSlice applies the provided transformation function `fn` to each element of the input slice `s` and returns a new slice of the resulting values.
Example ¶
result := MapSlice([]int{1, 2, 3}, func(x int) string { return fmt.Sprint(x) })
fmt.Println(result)
Output: [1 2 3]
Types ¶
type Set ¶
type Set[T comparable] map[T]struct{}
Set is a generic implementation of a mathematical set for comparable types. It is implemented as a map with empty struct values for minimal memory usage.
func CollectToSet ¶ added in v0.5.0
func CollectToSet[T comparable](seq iter.Seq[T]) Set[T]
CollectToSet collects elements from the given sequence into a Set.
Example ¶
seq := slices.Values([]int{1, 2, 2, 3})
s := CollectToSet(seq)
for v := range s {
fmt.Println(v)
}
Output: 1 2 3
func SetOf ¶
func SetOf[T comparable](elems ...T) Set[T]
SetOf creates a new Set containing the given elements. It is a shorthand for ToSet with variadic arguments.
Example ¶
s := SetOf(1, 2, 3)
for v := range s {
fmt.Println(v)
}
Output: 1 2 3
func ToSet ¶
func ToSet[T comparable](slice []T) Set[T]
ToSet converts a slice into a Set, eliminating duplicates.
Example ¶
s := ToSet([]string{"a", "b", "a"})
for v := range s {
fmt.Println(v)
}
Output: a b
func (Set[T]) Add ¶
Add inserts an element into the Set. Returns the Set to allow chaining.
Example ¶
s := SetOf(1)
s.Add(2).Add(3)
for v := range s {
fmt.Println(v)
}
Output: 1 2 3
func (Set[T]) AddSeq ¶ added in v0.5.0
AddSeq inserts all elements from the given sequence to the Set. Returns the Set to allow chaining.
Example ¶
s := SetOf(1)
s.AddSeq(slices.Values([]int{2, 3, 4}))
for v := range s {
fmt.Println(v)
}
Output: 1 2 3 4
func (Set[T]) AddSlice ¶ added in v0.5.0
AddSlice inserts all elements from the given slice to the Set. Returns the Set to allow chaining.
Example ¶
s := SetOf("a")
s.AddSlice([]string{"b", "c"})
for v := range s {
fmt.Println(v)
}
Output: a b c
func (Set[T]) All ¶ added in v0.5.0
All returns a sequence containing all elements in the Set. The order is not guaranteed.
Example ¶
s := SetOf(1, 2, 3)
for v := range s.All() {
fmt.Println(v)
}
Output: 1 2 3
func (Set[T]) Contains ¶
Contains checks whether an element exists in the Set.
Example ¶
s := SetOf("apple", "banana")
fmt.Println(s.Contains("banana"))
fmt.Println(s.Contains("orange"))
Output: true false
func (Set[T]) Diff ¶
Diff returns a new Set containing elements that are defined in current Set but not in the other set.
Example ¶
a := SetOf(1, 2, 3)
b := SetOf(2, 3, 4)
diff := a.Diff(b)
for v := range diff {
fmt.Println(v)
}
Output: 1
func (Set[T]) Intersect ¶
Intersect returns a new Set containing only elements present in both Sets.
Example ¶
a := SetOf(1, 2, 3)
b := SetOf(2, 3, 4)
intersection := a.Intersect(b)
for v := range intersection {
fmt.Println(v)
}
Output: 2 3
func (Set[T]) Intersects ¶
Intersects returns true if there is at least one common element between the Sets.
Example ¶
a := SetOf("x", "y")
b := SetOf("y", "z")
fmt.Println(a.Intersects(b))
Output: true
func (Set[T]) Join ¶
Join adds all elements from another Set into the current Set (union). Returns the modified Set to allow chaining.
Example ¶
a := SetOf(1, 2)
b := SetOf(2, 3)
a.Join(b)
for v := range a {
fmt.Println(v)
}
Output: 1 2 3
func (Set[T]) SortedValues ¶
SortedValues returns a sorted slice containing all elements in the Set.
Example ¶
s := SetOf(3, 1, 2)
for _, v := range s.SortedValues(cmp.Compare) {
fmt.Println(v)
}
Output: 1 2 3