Documentation ¶
Index ¶
- type Sequence
- func (seq *Sequence[T]) At(i int) T
- func (seq *Sequence[T]) ForEach(fn func(T)) *Sequence[T]
- func (seq *Sequence[T]) ForEachIndex(fn func(T, int)) *Sequence[T]
- func (seq *Sequence[T]) Len() int
- func (seq *Sequence[T]) Slice() []T
- func (seq *Sequence[T]) SliceFunc(fn func(T) T) []T
- func (seq *Sequence[T]) SliceFuncFilter(fn func(T) (T, bool)) []T
- func (seq *Sequence[T]) SliceFuncIndex(fn func(T, int) T) []T
- type Sequenceable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Sequence ¶
type Sequence[T any] struct { // contains filtered or unexported fields }
Sequence represents a type that can be expressed as a sequence of elements of type [T]. Under the hood, it uses a length function to get the number of elements and a getter to retrieve each one.
Concurrent use of Sequence is safe as long as the provided length and getter functions are concurrent-safe, along with any operations performed on the sequence, such as those in the Sequence.ForEach method
func FromSequenceable ¶
func FromSequenceable[T any](s Sequenceable[T]) *Sequence[T]
FromSequenceable returns a new Sequence from the given value that implements the Sequenceable interface.
Example:
import ( "go/types" "github.com/marcozac/go-aliaser/util/sequence" ) var tuple *types.Tuple seq := sequence.FromSequenceable(tuple)
func (*Sequence[T]) ForEachIndex ¶
ForEachIndex calls the given function for each element in the sequence as Sequence.ForEach, but also provides the index of the element. It avoids the need to use a closure to capture the index.
func (*Sequence[T]) Slice ¶
func (seq *Sequence[T]) Slice() []T
Slice returns a new slice with the same length and the elements of the sequence.
func (*Sequence[T]) SliceFunc ¶
func (seq *Sequence[T]) SliceFunc(fn func(T) T) []T
SliceFunc returns a new slice with the results of calling the given function for each element in the sequence. It always returns a slice with the same length as the original sequence. If you want to exclude some elements, for example, zero values or in case of an error, use the Sequence.SliceFuncFilter method.
func (*Sequence[T]) SliceFuncFilter ¶
SliceFuncFilter returns a new slice with the results of calling the given function for each element in the sequence, whose result is appended to the slice only if the second value is true. The slice is clipped to remove unused capacity.
func (*Sequence[T]) SliceFuncIndex ¶
SliceFuncIndex returns a new slice with the results of calling the given function for each element in the sequence, as Sequence.SliceFunc, but also provides the index of the element, avoiding the need to use a closure to capture the index.
type Sequenceable ¶
Sequenceable is a generic interface for types that can be expressed as a sequence of elements. It is used to provide a common interface for types that are not slices or arrays, but can be used as a sequence.