Documentation ¶
Overview ¶
Package sequence implements support for a generic ordered sequence. A Sequence is a Collection that wraps an underlying Go slice and provides convenience methods and syntatic sugar on top of it.
Compared to a List, a Sequence allows for efficient O(1) access to arbitrary elements but slower insertion and removal time, making it ideal for situations where fast random access is needed.
for comparable types it is recommended to use ComparableSequence which provides additional methods
Index ¶
- type ComparableSequence
- func (c *ComparableSequence[T]) Contains(v T) bool
- func (c *ComparableSequence[T]) Diff(s *ComparableSequence[T]) *ComparableSequence[T]
- func (c *ComparableSequence[T]) Distinct() *ComparableSequence[T]
- func (c *ComparableSequence[T]) Equals(c2 *ComparableSequence[T]) bool
- func (c *ComparableSequence[T]) Exists(v T) bool
- func (c *ComparableSequence[T]) IndexOf(v T) int
- func (c *ComparableSequence[T]) LastIndexOf(v T) int
- func (c *ComparableSequence[T]) Max() T
- func (c *ComparableSequence[T]) Min() T
- func (c *ComparableSequence[T]) New(s ...[]T) collection.Collection[T]
- func (c *ComparableSequence[T]) NewOrdered(s ...[]T) collection.OrderedCollection[T]
- func (c *ComparableSequence[T]) Sum() T
- type Sequence
- func (c *Sequence[T]) Add(v T)
- func (c *Sequence[T]) All() iter.Seq2[int, T]
- func (c *Sequence[T]) Apply(f func(T) T) *Sequence[T]
- func (c *Sequence[T]) At(index int) T
- func (c *Sequence[T]) Backward() iter.Seq2[int, T]
- func (c *Sequence[T]) Clone() *Sequence[T]
- func (c *Sequence[T]) Concat(sequences ...Sequence[T]) *Sequence[T]
- func (c *Sequence[T]) Contains(f func(T) bool) bool
- func (c *Sequence[T]) Corresponds(s *Sequence[T], f func(T, T) bool) bool
- func (c *Sequence[T]) Count(f func(T) bool) int
- func (c *Sequence[T]) Dequeue() (T, error)
- func (c *Sequence[T]) Distinct(f func(T, T) bool) *Sequence[T]
- func (c *Sequence[T]) Drop(n int) *Sequence[T]
- func (c *Sequence[T]) DropRight(n int) *Sequence[T]
- func (c *Sequence[T]) DropWhile(f func(T) bool) *Sequence[T]
- func (c *Sequence[T]) Enqueue(v T)
- func (c *Sequence[T]) Equals(c2 *Sequence[T], f func(T, T) bool) bool
- func (c *Sequence[T]) Exists(f func(T) bool) bool
- func (c *Sequence[T]) Filter(f func(T) bool) *Sequence[T]
- func (c *Sequence[T]) FilterNot(f func(T) bool) *Sequence[T]
- func (c *Sequence[T]) Find(f func(T) bool) (int, T)
- func (c *Sequence[T]) FindLast(f func(T) bool) (int, T)
- func (c *Sequence[T]) ForAll(f func(T) bool) bool
- func (c *Sequence[T]) Head() (T, error)
- func (c *Sequence[T]) Init() *Sequence[T]
- func (c *Sequence[T]) IsEmpty() bool
- func (c *Sequence[T]) Last() (T, error)
- func (c *Sequence[T]) Length() int
- func (c *Sequence[T]) New(s ...[]T) collection.Collection[T]
- func (c *Sequence[T]) NewOrdered(s ...[]T) collection.OrderedCollection[T]
- func (c *Sequence[T]) NonEmpty() bool
- func (c *Sequence[T]) Partition(f func(T) bool) (*Sequence[T], *Sequence[T])
- func (c *Sequence[T]) Pop() (T, error)
- func (c *Sequence[T]) Push(v T)
- func (c *Sequence[T]) Random() T
- func (c *Sequence[T]) Reverse() *Sequence[T]
- func (c *Sequence[T]) Slice(start, end int) collection.OrderedCollection[T]
- func (c *Sequence[T]) SplitAt(n int) (*Sequence[T], *Sequence[T])
- func (c *Sequence[T]) String() string
- func (c *Sequence[T]) Tail() *Sequence[T]
- func (c *Sequence[T]) Take(n int) *Sequence[T]
- func (c *Sequence[T]) TakeRight(n int) *Sequence[T]
- func (c *Sequence[T]) ToSlice() []T
- func (c *Sequence[T]) Values() iter.Seq[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ComparableSequence ¶
ComparableSequence is a sequence of comparable types. it is similar to Sequence, but with additional methods that do not require a higher order function comparator to be provided as an argument: Max(), Min(), Sum(), Distinct(), Diff(c), and Exists(v).
func NewComparableSequence ¶
func NewComparableSequence[T cmp.Ordered](s ...[]T) *ComparableSequence[T]
NewComparableSequence is a constructor for a sequence of comparable types.
func (*ComparableSequence[T]) Contains ¶
func (c *ComparableSequence[T]) Contains(v T) bool
Contains returns true if the sequence contains the given value.
func (*ComparableSequence[T]) Diff ¶
func (c *ComparableSequence[T]) Diff(s *ComparableSequence[T]) *ComparableSequence[T]
func (*ComparableSequence[T]) Distinct ¶
func (c *ComparableSequence[T]) Distinct() *ComparableSequence[T]
Distinct returns a new sequence containing only the unique elements from the original sequence.
func (*ComparableSequence[T]) Equals ¶
func (c *ComparableSequence[T]) Equals(c2 *ComparableSequence[T]) bool
Equals returns true if the two sequences are equal.
func (*ComparableSequence[T]) Exists ¶
func (c *ComparableSequence[T]) Exists(v T) bool
Exists returns true if the sequence contains the given value.
func (*ComparableSequence[T]) IndexOf ¶
func (c *ComparableSequence[T]) IndexOf(v T) int
IndexOf returns the index of the first occurrence of the specified element in this sequence, or -1 if this sequence does not contain the element.
func (*ComparableSequence[T]) LastIndexOf ¶
func (c *ComparableSequence[T]) LastIndexOf(v T) int
LastIndexOf returns the index of the last occurrence of the specified element in this sequence, or -1 if this sequence does not contain the element.
func (*ComparableSequence[T]) Max ¶
func (c *ComparableSequence[T]) Max() T
func (*ComparableSequence[T]) Min ¶
func (c *ComparableSequence[T]) Min() T
func (*ComparableSequence[T]) New ¶
func (c *ComparableSequence[T]) New(s ...[]T) collection.Collection[T]
func (*ComparableSequence[T]) NewOrdered ¶
func (c *ComparableSequence[T]) NewOrdered(s ...[]T) collection.OrderedCollection[T]
func (*ComparableSequence[T]) Sum ¶
func (c *ComparableSequence[T]) Sum() T
type Sequence ¶
type Sequence[T any] struct { // contains filtered or unexported fields }
func NewSequence ¶
func (*Sequence[T]) Backward ¶
Backward returns an iterator over all elements of the sequence in reverse order.
func (*Sequence[T]) Contains ¶
Contains tests whether a predicate holds for at least one element of this sequence.
func (*Sequence[T]) Corresponds ¶
Corresponds is an alias for collection.Corresponds
func (*Sequence[T]) Distinct ¶
Distinct takes an "equality" function as an argument and returns a new sequence containing all the unique elements If you prefer not to pass an equality function use a ComparableSequence.
func (*Sequence[T]) Enqueue ¶
func (c *Sequence[T]) Enqueue(v T)
Enqueue appends an element to the sequence.
func (*Sequence[T]) Equals ¶
Equals takes a sequence and an equality function as an argument and returns true if the two sequences are equal. If you prefer not to pass an equality function use a ComparableSequence.
func (*Sequence[T]) New ¶
func (c *Sequence[T]) New(s ...[]T) collection.Collection[T]
New is a constructor for a generic sequence.
func (*Sequence[T]) NewOrdered ¶
func (c *Sequence[T]) NewOrdered(s ...[]T) collection.OrderedCollection[T]
NewOrdered returns a new ordered collection.
func (*Sequence[T]) Random ¶
func (c *Sequence[T]) Random() T
Random returns a random element from the sequence.
func (*Sequence[T]) Slice ¶
func (c *Sequence[T]) Slice(start, end int) collection.OrderedCollection[T]
Slice returns a new sequence containing the elements from the start index to the end index.