seq

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-seq

Go Reference

go-seq (pronounced "go seek") is a Go package for working with sequences.

Go 1.23 introduced iterators and the iter.Seq and iter.Seq2 sequence types. This package provides functions to transform, filter, collect, and reduce sequences.

Install

go get github.com/arielsrv/go-seq

Documentation

Overview

Package seq (go-seq, pronounced "go seek") has functions for working with iterator sequences, iter.Seq and iter.Seq2.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aggregate

func Aggregate[V, A any](seq iter.Seq[V], init A, f func(A, V) A) A

Aggregate applies an accumulator function over a sequence.

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

func All[V any](seq iter.Seq[V], f func(V) bool) bool

All determines if all values of a sequence satisfy a condition.

This returns true if the sequence was empty.

func Any

func Any[V any](seq iter.Seq[V]) bool

Any determines if a sequence has any values.

func AnyFunc

func AnyFunc[V any](seq iter.Seq[V], f func(V) bool) bool

AnyFunc determines if a sequence has any value that satisfies a predicate.

func Append

func Append[V any](seq iter.Seq[V], vals ...V) iter.Seq[V]

Append adds values to the end of a sequence.

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 Chunk

func Chunk[V any](seq iter.Seq[V], size int) iter.Seq[[]V]

Chunk splits the values of a sequence into slices of a given size at most.

func Collect

func Collect[V any](seq iter.Seq[V]) []V

Collect collects values from a sequence into a new slice.

func CollectLast

func CollectLast[V any](seq iter.Seq[V], n int) []V

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 Concat

func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V]

Concat concatenates multiples sequences into a single sequence.

func Concat2

func Concat2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]

Concat2 concatenates multiple key-value sequences into a single key-value sequence.

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

func Count[V any](seq iter.Seq[V]) int

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

func CountFunc[V any](seq iter.Seq[V], f func(V) bool) int

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

func CountFuncGrouped[K comparable, V any](
	seq iter.Seq2[K, V],
	f func(K, V) bool,
) map[K]int

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

func DistinctKeys[K comparable, V any](seq iter.Seq2[K, V]) iter.Seq2[K, V]

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 Empty

func Empty[V any]() iter.Seq[V]

Empty returns an empty sequence.

func Empty2

func Empty2[K, V any]() iter.Seq2[K, V]

Empty2 returns an empty key-value sequence.

func Equal

func Equal[V comparable](seq1, seq2 iter.Seq[V]) bool

Equal determines if two sequences are equal.

func EqualFunc

func EqualFunc[V any](seq1, seq2 iter.Seq[V], f func(V, V) bool) bool

EqualFunc determines if two sequences are equal using a function to compare values.

func Except

func Except[V comparable](seq iter.Seq[V], vals Set[V]) iter.Seq[V]

Except returns values from a sequence that are not present in a set.

func ExceptKeys

func ExceptKeys[K comparable, V any](seq iter.Seq2[K, V], keys Set[K]) iter.Seq2[K, V]

ExceptKeys returns key-value pairs from a sequence when the key is not present in a set.

func First

func First[V any](seq iter.Seq[V]) (V, bool)

First returns the first value of a sequence.

A second return value indicates whether the sequence contained any values.

func FirstFunc

func FirstFunc[V any](seq iter.Seq[V], f func(V) bool) (V, bool)

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 Intersect

func Intersect[V comparable](seq iter.Seq[V], vals Set[V]) iter.Seq[V]

Intersect returns values from a sequence that are present in a set.

func IntersectKeys

func IntersectKeys[K comparable, V any](seq iter.Seq2[K, V], keys Set[K]) iter.Seq2[K, V]

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 Keys

func Keys[K, V any](seq iter.Seq2[K, V]) iter.Seq[K]

Keys returns the keys from a key-value sequence.

func Last

func Last[V any](seq iter.Seq[V]) (V, bool)

Last returns the last value of a sequence.

A second return value indicates whether the sequence contained any values.

func LastFunc

func LastFunc[V any](seq iter.Seq[V], f func(V) bool) (V, bool)

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

func Max[V cmp.Ordered](seq iter.Seq[V]) (V, bool)

Max returns the maximum value in a sequence.

A second return value indicates whether the sequence contained any values.

func MaxBy

func MaxBy[V any, K cmp.Ordered](seq iter.Seq[V], f func(V) K) (V, bool)

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

func MaxFunc[V any](seq iter.Seq[V], f func(V, V) int) (V, bool)

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

func Min[V cmp.Ordered](seq iter.Seq[V]) (V, bool)

Min returns the minimum value in a sequence.

A second return value indicates whether the sequence contained any values.

func MinBy

func MinBy[V any, K cmp.Ordered](seq iter.Seq[V], f func(V) K) (V, bool)

MinBy returns the minimum value in a sequence using a function to select a key to use for comparisons.

func MinFunc

func MinFunc[V any](seq iter.Seq[V], f func(V, V) int) (V, bool)

MinFunc returns the minimum value in a sequence using a comparison function.

A second return value indicates whether the sequence contained any values.

func OfType

func OfType[V, VOut any](seq iter.Seq[V]) iter.Seq[VOut]

OfType filters a sequence based on a type.

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 Prepend

func Prepend[V any](seq iter.Seq[V], vals ...V) iter.Seq[V]

Prepend adds values to the beginning of a sequence.

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 Repeat

func Repeat[V any](val V, n int) iter.Seq[V]

Repeat returns a sequence that yields a given value n numer of times.

func Reversed

func Reversed[V any](seq iter.Seq[V]) []V

Reversed collects values from a sequence into a new slice and then reverses it.

func Select

func Select[V, VOut any](seq iter.Seq[V], f func(V) VOut) iter.Seq[VOut]

Select projects each value of a sequence into a new value.

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

func SelectKeys[K, V any](seq iter.Seq[V], f func(V) K) iter.Seq2[K, V]

SelectKeys projects each value of a sequence into a key-value pair.

func SelectMany

func SelectMany[V, VOut any](seq iter.Seq[V], f func(V) iter.Seq[VOut]) iter.Seq[VOut]

SelectMany projects each value of a sequence into a sequence and then flattens the resulting sequences into a single sequence.

func SelectValues

func SelectValues[K, V, VOut any](seq iter.Seq2[K, V], f func(K, V) VOut) iter.Seq[VOut]

SelectValues projects each key-value pair of a sequence into a new value.

func Single

func Single[V any](seq iter.Seq[V]) (V, bool)

Single returns the only value in a sequence.

A second return value indicates whether the sequence contained exactly one value.

func SingleFunc

func SingleFunc[V any](seq iter.Seq[V], f func(V) bool) (V, bool)

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 Skip

func Skip[V any](seq iter.Seq[V], n int) iter.Seq[V]

Skip bypasses a given number of values in a sequence and returns the remaining values.

func SkipWhile

func SkipWhile[V any](seq iter.Seq[V], f func(int, V) bool) iter.Seq[V]

SkipWhile bypasses values in a sequence as long as a condition is true and then returns the remaining values.

func Sorted

func Sorted[V cmp.Ordered](seq iter.Seq[V]) []V

Sorted collects values from a sequence into a new slice and then sorts it.

func SortedBy

func SortedBy[V any, K cmp.Ordered](seq iter.Seq[V], f func(V) K) []V

SortedBy collects values from a sequence into a new slice and then uses a function to select a key for sorting.

func SortedFunc

func SortedFunc[V any](seq iter.Seq[V], f func(V, V) int) []V

SortedFunc collects values from a sequence into a new slice and then sorts it using the given comparison function.

func SortedStableBy

func SortedStableBy[V any, K cmp.Ordered](seq iter.Seq[V], f func(V) K) []V

SortedStableBy collects values from a sequence into a new slice and then uses a function to select a key for sorting.

func SortedStableFunc

func SortedStableFunc[V any](seq iter.Seq[V], f func(V, V) int) []V

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 Take

func Take[V any](seq iter.Seq[V], n int) iter.Seq[V]

Take returns a given number of values from the start of a sequence.

func TakeWhile

func TakeWhile[V any](seq iter.Seq[V], f func(int, V) bool) iter.Seq[V]

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

func ToSlice[V any](seq iter.Seq[V]) []V

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

func UnionKeys[K comparable, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]

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

func ValueAt[V any](seq iter.Seq[V], index int) (V, bool)

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 Values

func Values[K, V any](seq iter.Seq2[K, V]) iter.Seq[V]

Values returns the values from a key-value sequence.

func Where

func Where[V any](seq iter.Seq[V], f func(V) bool) iter.Seq[V]

Where filters a sequence based on a predicate.

func Where2

func Where2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) iter.Seq2[K, V]

Where2 filters key-value pairs based on a predicate.

func WithIndex

func WithIndex[V any](seq iter.Seq[V]) iter.Seq2[int, V]

WithIndex returns key-value pairs from a sequence of values by incorporating the sequence index as the key.

func Yield

func Yield[V any](vals ...V) iter.Seq[V]

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

func YieldBackwards[V any](vals ...V) iter.Seq[V]

YieldBackwards returns a sequence of values in reverse order.

Example:

// yields (3), (2), (1)
vals := seq.YieldBackwards(1, 2, 3)

func YieldChan

func YieldChan[V any](ch <-chan V) iter.Seq[V]

YieldChan returns a sequence of values from a channel.

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

func Zip[K, V any](keys iter.Seq[K], vals iter.Seq[V]) iter.Seq2[K, V]

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 NewSet

func NewSet[V comparable](vals ...V) Set[V]

NewSet creates a new set from values.

func (Set[V]) Add

func (s Set[V]) Add(v V) bool

Add adds a value to the set. Returns true if the value was added, false if it was already present.

func (Set[V]) Contains

func (s Set[V]) Contains(v V) bool

Contains determines whether a value is present in the set.

func (Set[V]) Remove

func (s Set[V]) Remove(v V) bool

Remove removes a value from the set. Returns true if the value was removed, false if it was not present.

func (Set[V]) Values

func (s Set[V]) Values() iter.Seq[V]

Values returns a sequence of values in the set.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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