sequence

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

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 New

func New[T any](len func() int, at func(int) T) *Sequence[T]

New returns a new Sequence with the given length and getter functions.

func (*Sequence[T]) At

func (seq *Sequence[T]) At(i int) T

At is a wrapper for the getter function.

func (*Sequence[T]) ForEach

func (seq *Sequence[T]) ForEach(fn func(T)) *Sequence[T]

ForEach calls the given function for each element in the sequence.

func (*Sequence[T]) ForEachIndex

func (seq *Sequence[T]) ForEachIndex(fn func(T, int)) *Sequence[T]

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]) Len

func (seq *Sequence[T]) Len() int

Len is a wrapper for the length function.

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

func (seq *Sequence[T]) SliceFuncFilter(fn func(T) (T, bool)) []T

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

func (seq *Sequence[T]) SliceFuncIndex(fn func(T, int) T) []T

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

type Sequenceable[T any] interface {
	Len() int
	At(int) T
}

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.

Jump to

Keyboard shortcuts

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