Documentation
¶
Overview ¶
Package seq (go-seq, pronounced "go seek") has functions for working with iterator sequences, iter.Seq and iter.Seq2.
Index ¶
- func Aggregate[V, A any](seq iter.Seq[V], init A, f func(A, V) A) A
- func AggregateGrouped[K comparable, V, A any](seq iter.Seq2[K, V], initFunc func(K) A, f func(A, V) A) map[K]A
- func All[V any](seq iter.Seq[V], f func(V) bool) bool
- func Any[V any](seq iter.Seq[V]) bool
- func AnyFunc[V any](seq iter.Seq[V], f func(V) bool) bool
- func Append[V any](seq iter.Seq[V], vals ...V) iter.Seq[V]
- func Average[V constraints.Integer | constraints.Float](seq iter.Seq[V]) float64
- func Chunk[V any](seq iter.Seq[V], size int) iter.Seq[[]V]
- func Collect[V any](seq iter.Seq[V]) []V
- func CollectLast[V any](seq iter.Seq[V], n int) []V
- func CollectMap[K comparable, V any](seq iter.Seq2[K, V]) map[K]V
- func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V]
- func Concat2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]
- func Contains[V comparable](seq iter.Seq[V], val V) bool
- func ContainsKey[K comparable, V any](seq iter.Seq2[K, V], key K) bool
- func Count[V any](seq iter.Seq[V]) int
- func CountFunc[V any](seq iter.Seq[V], f func(V) bool) int
- func CountFuncGrouped[K comparable, V any](seq iter.Seq2[K, V], f func(K, V) bool) map[K]int
- func CountGrouped[K comparable, V any](seq iter.Seq2[K, V]) map[K]int
- func Distinct[V comparable](seq iter.Seq[V]) iter.Seq[V]
- func DistinctKeys[K comparable, V any](seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func Empty[V any]() iter.Seq[V]
- func Empty2[K, V any]() iter.Seq2[K, V]
- func Equal[V comparable](seq1, seq2 iter.Seq[V]) bool
- func EqualFunc[V any](seq1, seq2 iter.Seq[V], f func(V, V) bool) bool
- func Except[V comparable](seq iter.Seq[V], vals Set[V]) iter.Seq[V]
- func ExceptKeys[K comparable, V any](seq iter.Seq2[K, V], keys Set[K]) iter.Seq2[K, V]
- func First[V any](seq iter.Seq[V]) (V, bool)
- func FirstFunc[V any](seq iter.Seq[V], f func(V) bool) (V, bool)
- func Grouped[K comparable, V any](seq iter.Seq2[K, V]) map[K][]V
- func Intersect[V comparable](seq iter.Seq[V], vals Set[V]) iter.Seq[V]
- func IntersectKeys[K comparable, V any](seq iter.Seq2[K, V], keys Set[K]) iter.Seq2[K, V]
- func Join[V1 any, K comparable, Map ~map[K]V2, V2 any, VOut any](seq iter.Seq[V1], m Map, selectKey func(V1) K, f func(V1, V2) VOut) iter.Seq[VOut]
- func Keys[K, V any](seq iter.Seq2[K, V]) iter.Seq[K]
- func Last[V any](seq iter.Seq[V]) (V, bool)
- func LastFunc[V any](seq iter.Seq[V], f func(V) bool) (V, bool)
- func Max[V cmp.Ordered](seq iter.Seq[V]) (V, bool)
- func MaxBy[V any, K cmp.Ordered](seq iter.Seq[V], f func(V) K) (V, bool)
- func MaxFunc[V any](seq iter.Seq[V], f func(V, V) int) (V, bool)
- func Min[V cmp.Ordered](seq iter.Seq[V]) (V, bool)
- func MinBy[V any, K cmp.Ordered](seq iter.Seq[V], f func(V) K) (V, bool)
- func MinFunc[V any](seq iter.Seq[V], f func(V, V) int) (V, bool)
- func OfType[V, VOut any](seq iter.Seq[V]) iter.Seq[VOut]
- func OuterJoin[V1 any, K comparable, Map ~map[K]V2, V2 any, VOut any](seq iter.Seq[V1], m Map, selectKey func(V1) K, f func(V1, V2, bool) VOut) iter.Seq[VOut]
- func Prepend[V any](seq iter.Seq[V], vals ...V) iter.Seq[V]
- func Range[V constraints.Integer | constraints.Float](start, end, step V) (iter.Seq[V], error)
- func Repeat[V any](val V, n int) iter.Seq[V]
- func Reversed[V any](seq iter.Seq[V]) []V
- func Select[V, VOut any](seq iter.Seq[V], f func(V) VOut) iter.Seq[VOut]
- func Select2[K, V, KOut, VOut any](seq iter.Seq2[K, V], f func(K, V) (KOut, VOut)) iter.Seq2[KOut, VOut]
- func SelectKeys[K, V any](seq iter.Seq[V], f func(V) K) iter.Seq2[K, V]
- func SelectMany[V, VOut any](seq iter.Seq[V], f func(V) iter.Seq[VOut]) iter.Seq[VOut]
- func SelectValues[K, V, VOut any](seq iter.Seq2[K, V], f func(K, V) VOut) iter.Seq[VOut]
- func Single[V any](seq iter.Seq[V]) (V, bool)
- func SingleFunc[V any](seq iter.Seq[V], f func(V) bool) (V, bool)
- func Skip[V any](seq iter.Seq[V], n int) iter.Seq[V]
- func SkipWhile[V any](seq iter.Seq[V], f func(int, V) bool) iter.Seq[V]
- func Sorted[V cmp.Ordered](seq iter.Seq[V]) []V
- func SortedBy[V any, K cmp.Ordered](seq iter.Seq[V], f func(V) K) []V
- func SortedFunc[V any](seq iter.Seq[V], f func(V, V) int) []V
- func SortedStableBy[V any, K cmp.Ordered](seq iter.Seq[V], f func(V) K) []V
- func SortedStableFunc[V any](seq iter.Seq[V], f func(V, V) int) []V
- func Sum[V constraints.Integer | constraints.Float](seq iter.Seq[V]) V
- func Take[V any](seq iter.Seq[V], n int) iter.Seq[V]
- func TakeWhile[V any](seq iter.Seq[V], f func(int, V) bool) iter.Seq[V]
- func ToMap[K comparable, V any](seq iter.Seq2[K, V]) map[K]V
- func ToSlice[V any](seq iter.Seq[V]) []V
- func Union[V comparable](seqs ...iter.Seq[V]) iter.Seq[V]
- func UnionKeys[K comparable, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]
- func ValueAt[V any](seq iter.Seq[V], index int) (V, bool)
- func Values[K, V any](seq iter.Seq2[K, V]) iter.Seq[V]
- func Where[V any](seq iter.Seq[V], f func(V) bool) iter.Seq[V]
- func Where2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) iter.Seq2[K, V]
- func WithIndex[V any](seq iter.Seq[V]) iter.Seq2[int, V]
- func Yield[V any](vals ...V) iter.Seq[V]
- func YieldBackwards[V any](vals ...V) iter.Seq[V]
- func YieldChan[V any](ch <-chan V) iter.Seq[V]
- func YieldKeyValues[Map ~map[K]V, K comparable, V any](m Map) iter.Seq2[K, V]
- func Zip[K, V any](keys iter.Seq[K], vals iter.Seq[V]) iter.Seq2[K, V]
- type Set
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AggregateGrouped ¶
func AggregateGrouped[K comparable, V, A any]( seq iter.Seq2[K, V], initFunc func(K) A, f func(A, V) A, ) map[K]A
AggregateGrouped aggregates values from a sequence of key-value pairs into a map with accumulated values grouped by key.
func All ¶
All determines if all values of a sequence satisfy a condition.
This returns true if the sequence was empty.
func Average ¶
func Average[V constraints.Integer | constraints.Float](seq iter.Seq[V]) float64
Average computes the average of the values in a sequence.
func CollectLast ¶
CollectLast collects values from a sequence into a new slice, keeping only the last n values.
func CollectMap ¶
func CollectMap[K comparable, V any](seq iter.Seq2[K, V]) map[K]V
CollectMap collects a sequence of key-value pairs into a map. If there are duplicate keys, the last value for the key is kept.
func Contains ¶
func Contains[V comparable](seq iter.Seq[V], val V) bool
Contains determines if a sequence contains a value.
func ContainsKey ¶
func ContainsKey[K comparable, V any](seq iter.Seq2[K, V], key K) bool
ContainsKey determines whether a key is present in a key-value sequence.
func Count ¶
Count returns the number of values in a sequence.
This iterates over the entire sequence to count the values. Use Any instead if you only need to check whether the sequence has any values. Use Single instead if you need to check whether the sequence has exactly one value.
func CountFunc ¶
CountFunc returns the number of values in a sequence that satisfy a predicate.
This iterates over the entire sequence to count the values. Use AnyFunc instead if you only need to check whether the sequence has any values that satisfy a predicate. Use SingleFunc instead if you need to check whether the sequence has exactly one value that satisfies a predicate.
func CountFuncGrouped ¶
CountFuncGrouped counts the number of occurrences of each key in a sequence of key-value pairs based on a predicate.
func CountGrouped ¶
func CountGrouped[K comparable, V any](seq iter.Seq2[K, V]) map[K]int
CountGrouped counts the number of occurrences of each key in a sequence of key-value pairs.
func Distinct ¶
func Distinct[V comparable](seq iter.Seq[V]) iter.Seq[V]
Distinct returns distinct values from a sequence.
The first occurrence is yielded, and any subsequent occurrences are ignored.
func DistinctKeys ¶
DistinctKeys returns distinct key-value pairs from a sequence by comparing the keys.
The first occurrence is yielded, and any subsequent occurrences are ignored.
func Equal ¶
func Equal[V comparable](seq1, seq2 iter.Seq[V]) bool
Equal determines if two sequences are equal.
func EqualFunc ¶
EqualFunc determines if two sequences are equal using a function to compare values.
func ExceptKeys ¶
ExceptKeys returns key-value pairs from a sequence when the key is not present in a set.
func First ¶
First returns the first value of a sequence.
A second return value indicates whether the sequence contained any values.
func FirstFunc ¶
FirstFunc returns the first value of a sequence that satisfies a predicate.
A second return value indicates whether the sequence contained any value that satisfied the predicate.
func Grouped ¶
func Grouped[K comparable, V any](seq iter.Seq2[K, V]) map[K][]V
Grouped collects a sequence of key-value pairs into a map with values grouped by key into slices.
func IntersectKeys ¶
IntersectKeys returns key-value pairs from a sequence when the key is present in a set.
func Join ¶
func Join[V1 any, K comparable, Map ~map[K]V2, V2 any, VOut any]( seq iter.Seq[V1], m Map, selectKey func(V1) K, f func(V1, V2) VOut, ) iter.Seq[VOut]
Join joins a sequence with values from a map using a function to select the key and a function to project the values to a new value.
The resulting sequence will only contain values where the key exists in the map.
Example:
var posts iter.Seq[*post.Post] var users map[user.UserID]*user.User postUsers := seq.Join(posts, users, func(p *post.Post) user.UserID { return p.UserID }, func(p *post.Post, u *user.User) *post.PostUser { return post.NewPostUser(p, u) }, )
func Last ¶
Last returns the last value of a sequence.
A second return value indicates whether the sequence contained any values.
func LastFunc ¶
LastFunc returns the last value of a sequence that satisfies a predicate.
A second return value indicates whether the sequence contained any value that satisfied the predicate.
func Max ¶
Max returns the maximum value in a sequence.
A second return value indicates whether the sequence contained any values.
func MaxBy ¶
MaxBy returns the maximum value in a sequence using a function to select a key to use for comparisons.
A second return value indicates whether the sequence contained any values.
func MaxFunc ¶
MaxFunc returns the maximum value in a sequence using a comparison function.
A second return value indicates whether the sequence contained any values.
func Min ¶
Min returns the minimum value in a sequence.
A second return value indicates whether the sequence contained any values.
func MinBy ¶
MinBy returns the minimum value in a sequence using a function to select a key to use for comparisons.
func MinFunc ¶
MinFunc returns the minimum value in a sequence using a comparison function.
A second return value indicates whether the sequence contained any values.
func OuterJoin ¶
func OuterJoin[V1 any, K comparable, Map ~map[K]V2, V2 any, VOut any]( seq iter.Seq[V1], m Map, selectKey func(V1) K, f func(V1, V2, bool) VOut, ) iter.Seq[VOut]
OuterJoin joins a sequence with values from a map using a function to select the key and a function to project the values to a new value.
The resulting sequence will contain all values from the sequence. A boolean flag is provided to indicate if the key was found in the map.
func Range ¶
func Range[V constraints.Integer | constraints.Float](start, end, step V) (iter.Seq[V], error)
Range returns a sequence of numbers from start to end (inclusive) incremented by a given step size.
If iteration of the sequence is stopped early and resumed, the sequence will start from the beginning.
func Select2 ¶
func Select2[K, V, KOut, VOut any](seq iter.Seq2[K, V], f func(K, V) (KOut, VOut)) iter.Seq2[KOut, VOut]
Select2 projects each key-value pair of a sequence into a new key-value pair.
func SelectKeys ¶
SelectKeys projects each value of a sequence into a key-value pair.
func SelectMany ¶
SelectMany projects each value of a sequence into a sequence and then flattens the resulting sequences into a single sequence.
func SelectValues ¶
SelectValues projects each key-value pair of a sequence into a new value.
func Single ¶
Single returns the only value in a sequence.
A second return value indicates whether the sequence contained exactly one value.
func SingleFunc ¶
SingleFunc returns the only value in a sequence that satisfies a predicate.
A second return value indicates whether the sequence contained exactly one value that satisfied the predicate.
func SkipWhile ¶
SkipWhile bypasses values in a sequence as long as a condition is true and then returns the remaining values.
func SortedBy ¶
SortedBy collects values from a sequence into a new slice and then uses a function to select a key for sorting.
func SortedFunc ¶
SortedFunc collects values from a sequence into a new slice and then sorts it using the given comparison function.
func SortedStableBy ¶
SortedStableBy collects values from a sequence into a new slice and then uses a function to select a key for sorting.
func SortedStableFunc ¶
SortedStableFunc collects values from a sequence into a new slice and then sorts it using the given comparison function maintaining the order of equal values.
func Sum ¶
func Sum[V constraints.Integer | constraints.Float](seq iter.Seq[V]) V
Sum computes the sum of the values in a sequence.
func TakeWhile ¶
TakeWhile returns values from a sequence as long as a given condition is true and then skips the remaining values.
func ToMap ¶ added in v0.0.3
func ToMap[K comparable, V any](seq iter.Seq2[K, V]) map[K]V
ToMap converts a sequence of key-value pairs to a map.
This iterates over the entire sequence to collect all key-value pairs. Use with caution for very large sequences as it loads all values into memory. If the sequence contains duplicate keys, later values will overwrite earlier ones.
func ToSlice ¶ added in v0.0.3
ToSlice converts a sequence to a slice.
This iterates over the entire sequence to collect all values. Use with caution for very large sequences as it loads all values into memory.
func Union ¶
func Union[V comparable](seqs ...iter.Seq[V]) iter.Seq[V]
Union returns the set union of multiple sequences.
The first occurrence is yielded, and any subsequent occurrences are ignored.
func UnionKeys ¶
UnionKeys returns the set union of multiple sequences of key-value pairs by comparing keys.
The first occurrence is yielded, and any subsequent occurrences are ignored.
func ValueAt ¶
ValueAt returns the value at a given index in a sequence.
A second return value indicates whether the given index was within the bounds of the sequence. This panics if the given index is negative.
func WithIndex ¶
WithIndex returns key-value pairs from a sequence of values by incorporating the sequence index as the key.
func Yield ¶
Yield returns a sequence of values.
This is useful for creating a sequence from a slice or variadic arguments.
Examples:
// yield each element of the slice // yields ("a"), ("b"), ("c") letters := []string{"a", "b", "c"} vals := seq.Yield(letters...) // yields (1), (2), (3) vals := seq.Yield(1, 2, 3)
func YieldBackwards ¶
YieldBackwards returns a sequence of values in reverse order.
Example:
// yields (3), (2), (1) vals := seq.YieldBackwards(1, 2, 3)
func YieldKeyValues ¶
func YieldKeyValues[Map ~map[K]V, K comparable, V any](m Map) iter.Seq2[K, V]
YieldKeyValues returns a sequence that yields the key-value pairs from a map.
The iteration order is not specified and is not guaranteed to be the same from one call to the next.
func Zip ¶
Zip returns a key-value sequence that combines values from two value sequences. The first sequence is used as the key and the second sequence is used as the value.
The resulting sequence will be as long as the shortest given sequence. Any remaining values in the longer sequence are ignored.
Example:
seqK := seq.Yield(1, 2, 3) seqV := seq.Yield("a", "b", "c", "d") // yields (1, "a"), (2, "b"), (3, "c") kvs := seq.Zip(seqK, seqV)
Types ¶
type Set ¶
type Set[V comparable] map[V]struct{}
Set is a set of values.
func CollectSet ¶
func CollectSet[V comparable](seq iter.Seq[V]) Set[V]
CollectSet collects values from a sequence into a new set.
func (Set[V]) Add ¶
Add adds a value to the set. Returns true if the value was added, false if it was already present.