list

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: 5 Imported by: 0

Documentation

Overview

Package list implements support for a generic ordered List. A List is a Collection that wraps an underlying doubly linked list and provides convenience methods and syntatic sugar on top of it.

Compared to a Sequence, a List allows for efficient insertion and removal of elements at the edges of the list, but slower access to arbitrary elements. This makes the List a good choice for implementing queues or stacks.

For a list of comparable types, consider using ComparableList, which provides additional methods for comparable types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ComparableList

type ComparableList[T cmp.Ordered] struct {
	List[T]
}

ComparableList is a list of comparable types. it is similar to List, 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 NewComparableList

func NewComparableList[T cmp.Ordered](s ...[]T) *ComparableList[T]

func (*ComparableList[T]) Contains

func (l *ComparableList[T]) Contains(v T) bool

Contains returns true if the list contains the given value.

func (*ComparableList[T]) Diff

func (l *ComparableList[T]) Diff(s *ComparableList[T]) *ComparableList[T]

Diff returns a new list containing the elements of the original list that are not in the other list.

func (*ComparableList[T]) Distinct

func (l *ComparableList[T]) Distinct() *ComparableList[T]

Distinct returns a new list containing only the unique elements from the original list.

func (*ComparableList[T]) Equals

func (l *ComparableList[T]) Equals(s *ComparableList[T]) bool

Equals returns true if the two lists are equal.

func (*ComparableList[T]) Exists

func (l *ComparableList[T]) Exists(v T) bool

Exists is an alias for Contains

func (*ComparableList[T]) IndexOf

func (l *ComparableList[T]) IndexOf(v T) int

IndexOf returns the index of the first occurrence of the specified element in this list,

func (*ComparableList[T]) LastIndexOf

func (l *ComparableList[T]) LastIndexOf(v T) int

LastIndexOf returns the index of the last occurrence of the specified element in this list,

func (*ComparableList[T]) Max

func (l *ComparableList[T]) Max() (T, error)

Max returns the maximum element in the list.

func (*ComparableList[T]) Min

func (l *ComparableList[T]) Min() (T, error)

Min returns the minimum element in the list.

func (*ComparableList[T]) New

func (c *ComparableList[T]) New(s ...[]T) collection.Collection[T]

func (*ComparableList[T]) NewOrdered

func (c *ComparableList[T]) NewOrdered(s ...[]T) collection.OrderedCollection[T]

func (*ComparableList[T]) Sum

func (l *ComparableList[T]) Sum() T

Sum returns the sum of the elements in the list.

type List

type List[T any] struct {
	// contains filtered or unexported fields
}

func NewList

func NewList[T any](s ...[]T) *List[T]

func (*List[T]) Add

func (l *List[T]) Add(v T)

Add adds a value to the end of the list.

func (*List[T]) All

func (l *List[T]) All() iter.Seq2[int, T]

All returns an index/value iterator for all nodes in the list.

func (*List[T]) Apply

func (l *List[T]) Apply(f func(T) T) *List[T]

Apply applies a function to each element in the list.

func (*List[T]) At

func (l *List[T]) At(index int) T

At returns the value of the node at the given index.

func (*List[T]) Backward

func (l *List[T]) Backward() iter.Seq2[int, T]

Backward returns an index/value iterator for all nodes in the list in reverse order.

func (*List[T]) Clone

func (l *List[T]) Clone() *List[T]

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

func (*List[T]) Concat

func (l *List[T]) Concat(lists ...*List[T]) *List[T]

Concat returns a new list concatenating the passed in lists.

func (*List[T]) Contains

func (l *List[T]) Contains(f func(T) bool) bool

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

func (*List[T]) Corresponds

func (l *List[T]) Corresponds(s *List[T], f func(T, T) bool) bool

Corresponds is an alias for collection.Corresponds

func (*List[T]) Count

func (l *List[T]) Count(f func(T) bool) int

Count is an alias for collection.Count

func (*List[T]) Dequeue

func (l *List[T]) Dequeue() (T, error)

Dequeue removes and returns the first element of the list.

func (*List[T]) Distinct

func (l *List[T]) Distinct(f func(T, T) bool) *List[T]

Distinct takes an "equality" function as an argument such as func(a T, b T) bool {return a == b} and returns a new sequence containing all the unique elements. If you don't want to pass an equality function use a ComparableList.

func (*List[T]) Drop

func (l *List[T]) Drop(n int) *List[T]

Drop is an alias for collection.Drop

func (*List[T]) DropRight

func (l *List[T]) DropRight(n int) *List[T]

DropRight is an alias for collection.DropRight

func (*List[T]) DropWhile

func (l *List[T]) DropWhile(f func(T) bool) *List[T]

DropWhile is an alias for collection.DropWhile

func (*List[T]) Enqueue

func (l *List[T]) Enqueue(v T)

Enqueue appends an element to the list.

func (*List[T]) Equals

func (l *List[T]) Equals(s *List[T], f func(T, T) bool) bool

Equals takes a list and an equality function as arguments and returns true if the two sequences are equal. If you prefer not to pass an equality function use a ComparableList.

func (*List[T]) Exists

func (l *List[T]) Exists(f func(T) bool) bool

Exists is an alias for Contains

func (*List[T]) Filter

func (l *List[T]) Filter(f func(T) bool) *List[T]

Filter is an alias for collection.Filter

func (*List[T]) FilterNot

func (l *List[T]) FilterNot(f func(T) bool) *List[T]

FilterNot is an alias for collection.FilterNot

func (*List[T]) Find

func (l *List[T]) Find(f func(T) bool) (int, T)

Find is an alias for collection.Find

func (*List[T]) FindLast

func (l *List[T]) FindLast(f func(T) bool) (int, T)

FindLast is an alias for collection.FindLast

func (*List[T]) ForAll

func (l *List[T]) ForAll(f func(T) bool) bool

ForAll is an alias for collection.ForAll

func (*List[T]) Head

func (l *List[T]) Head() (T, error)

Head is an alias for collection.Head

func (*List[T]) Init

func (l *List[T]) Init() *List[T]

Init is an alias for collection.Init

func (*List[T]) IsEmpty

func (l *List[T]) IsEmpty() bool

IsEmpty returns true if the list is empty.

func (*List[T]) Last

func (l *List[T]) Last() (T, error)

Last is an alias for collection.Last

func (*List[T]) Length

func (l *List[T]) Length() int

Length returns the number of nodes in the list.

func (*List[T]) New

func (l *List[T]) New(s ...[]T) collection.Collection[T]

New returns a new list.

func (*List[T]) NewOrdered

func (l *List[T]) NewOrdered(s ...[]T) collection.OrderedCollection[T]

NewOrdered returns a new ordered collection.

func (*List[T]) NonEmpty

func (l *List[T]) NonEmpty() bool

NonEmpty returns true if the list is not empty.

func (*List[T]) Partition

func (l *List[T]) Partition(f func(T) bool) (*List[T], *List[T])

Partition is an alias for collection.Partition

func (*List[T]) Pop

func (l *List[T]) Pop() (T, error)

Pop removes and returns the last element of the list.

func (*List[T]) Push

func (l *List[T]) Push(v T)

Push appends an element to the list.

func (*List[T]) Random

func (l *List[T]) Random() T

Random returns a random value from the list.

func (*List[T]) Reverse

func (l *List[T]) Reverse() *List[T]

Reverse is an alias for collection.Reverse

func (*List[T]) Slice

func (l *List[T]) Slice(start, end int) collection.OrderedCollection[T]

Slice returns a new list containing only the nodes between the start and end indices.

func (*List[T]) SplitAt

func (l *List[T]) SplitAt(n int) (*List[T], *List[T])

SplitAt splits the list at the given index.

func (*List[T]) String

func (l *List[T]) String() string

Implement the Stringer interface.

func (*List[T]) Tail

func (l *List[T]) Tail() *List[T]

Tail is an alias for collection.Tail

func (*List[T]) Take

func (l *List[T]) Take(n int) *List[T]

Take is an alias for collection.Take

func (*List[T]) TakeRight

func (l *List[T]) TakeRight(n int) *List[T]

TakeRight is an alias for collection.TakeRight

func (*List[T]) ToSlice

func (l *List[T]) ToSlice() []T

ToSlice returns a slice containing all values in the list.

func (*List[T]) Values

func (l *List[T]) Values() iter.Seq[T]

Values returns an iterator for all values in the list.

type Node

type Node[T any] struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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