sequence

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ComparableSequence

type ComparableSequence[T cmp.Ordered] struct {
	Sequence[T]
}

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 (*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 NewSequence[T any](s ...[]T) *Sequence[T]

func (*Sequence[T]) Add

func (c *Sequence[T]) Add(v T)

Add appends an element to the sequence.

func (*Sequence[T]) All

func (c *Sequence[T]) All() iter.Seq2[int, T]

All returns an iterator over all elements of the sequence.

func (*Sequence[T]) Apply

func (c *Sequence[T]) Apply(f func(T) T) *Sequence[T]

Apply applies a function to each element in the sequence.

func (*Sequence[T]) At

func (c *Sequence[T]) At(index int) T

At returns the element at the given index.

func (*Sequence[T]) Backward

func (c *Sequence[T]) Backward() iter.Seq2[int, T]

Backward returns an iterator over all elements of the sequence in reverse order.

func (*Sequence[T]) Clone

func (c *Sequence[T]) Clone() *Sequence[T]

Clone returns a copy of the collection. This is a shallow clone.

func (*Sequence[T]) Concat

func (c *Sequence[T]) Concat(sequences ...Sequence[T]) *Sequence[T]

Concat returns a new sequence concatenating the passed in sequences.

func (*Sequence[T]) Contains

func (c *Sequence[T]) Contains(f func(T) bool) bool

Contains tests whether a predicate holds for at least one element of this sequence.

func (*Sequence[T]) Corresponds

func (c *Sequence[T]) Corresponds(s *Sequence[T], f func(T, T) bool) bool

Corresponds is an alias for collection.Corresponds

func (*Sequence[T]) Count

func (c *Sequence[T]) Count(f func(T) bool) int

Count is an alias for collection.Count

func (*Sequence[T]) Dequeue

func (c *Sequence[T]) Dequeue() (T, error)

Dequeue removes and returns the first element of the sequence.

func (*Sequence[T]) Distinct

func (c *Sequence[T]) Distinct(f func(T, T) bool) *Sequence[T]

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

func (c *Sequence[T]) Drop(n int) *Sequence[T]

Drop is an alias for collection.Drop

func (*Sequence[T]) DropRight

func (c *Sequence[T]) DropRight(n int) *Sequence[T]

DropRight is an alias for collection.DropRight

func (*Sequence[T]) DropWhile

func (c *Sequence[T]) DropWhile(f func(T) bool) *Sequence[T]

DropWhile is an alias for collection.DropWhile

func (*Sequence[T]) Enqueue

func (c *Sequence[T]) Enqueue(v T)

Enqueue appends an element to the sequence.

func (*Sequence[T]) Equals

func (c *Sequence[T]) Equals(c2 *Sequence[T], f func(T, T) bool) bool

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

func (c *Sequence[T]) Exists(f func(T) bool) bool

Exists is an alias for Contains

func (*Sequence[T]) Filter

func (c *Sequence[T]) Filter(f func(T) bool) *Sequence[T]

Filter is an alias for collection.Filter

func (*Sequence[T]) FilterNot

func (c *Sequence[T]) FilterNot(f func(T) bool) *Sequence[T]

FilterNot is an alias for collection.FilterNot

func (*Sequence[T]) Find

func (c *Sequence[T]) Find(f func(T) bool) (int, T)

Find is an alias for collection.Find

func (*Sequence[T]) FindLast

func (c *Sequence[T]) FindLast(f func(T) bool) (int, T)

FindLast is an alias for collection.FindLast

func (*Sequence[T]) ForAll

func (c *Sequence[T]) ForAll(f func(T) bool) bool

ForAll is an alias for collection.ForAll

func (*Sequence[T]) Head

func (c *Sequence[T]) Head() (T, error)

Head is an alias for collection.Head

func (*Sequence[T]) Init

func (c *Sequence[T]) Init() *Sequence[T]

Init is an alias for collection.Init

func (*Sequence[T]) IsEmpty

func (c *Sequence[T]) IsEmpty() bool

func (*Sequence[T]) Last

func (c *Sequence[T]) Last() (T, error)

Last is an alias for collection.Last

func (*Sequence[T]) Length

func (c *Sequence[T]) Length() int

Length returns the number of elements in the sequence.

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

func (c *Sequence[T]) NonEmpty() bool

returns true if the sequence is not empty.

func (*Sequence[T]) Partition

func (c *Sequence[T]) Partition(f func(T) bool) (*Sequence[T], *Sequence[T])

Partition is an alias for collection.Partition

func (*Sequence[T]) Pop

func (c *Sequence[T]) Pop() (T, error)

Pop removes and returns the last element of the sequence.

func (*Sequence[T]) Push

func (c *Sequence[T]) Push(v T)

Push appends an element to the sequence.

func (*Sequence[T]) Random

func (c *Sequence[T]) Random() T

Random returns a random element from the sequence.

func (*Sequence[T]) Reverse

func (c *Sequence[T]) Reverse() *Sequence[T]

Reverse is an alias for collection.Reverse

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.

func (*Sequence[T]) SplitAt

func (c *Sequence[T]) SplitAt(n int) (*Sequence[T], *Sequence[T])

SplitAt splits the sequence at the given index.

func (*Sequence[T]) String

func (c *Sequence[T]) String() string

String implements the Stringer interface.

func (*Sequence[T]) Tail

func (c *Sequence[T]) Tail() *Sequence[T]

Tail is an alias for collection.Tail

func (*Sequence[T]) Take

func (c *Sequence[T]) Take(n int) *Sequence[T]

Take is an alias for collection.Take

func (*Sequence[T]) TakeRight

func (c *Sequence[T]) TakeRight(n int) *Sequence[T]

TakeRight is an alias for collection.TakeRight

func (*Sequence[T]) ToSlice

func (c *Sequence[T]) ToSlice() []T

ToSlice returns the underlying slice.

func (*Sequence[T]) Values

func (c *Sequence[T]) Values() iter.Seq[T]

Values returns an iterator over all values of the underlying slice.

Jump to

Keyboard shortcuts

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