golist

package module
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2025 License: MIT Imports: 3 Imported by: 1

README

golist

Typed lists for Go

This is an attempt for typed lists in Go. Basically, this is about how (in my opionon) lists should have been designed in the first place. My expectation is that sooner or later, typed lists will be part of the Go standard library. Until then this is a substitute, as I find working with untyped lists unnecessarily cumbersome and error prone.

The implementation merely wraps (untyped) Go standard lists. This library uses the same methodology and method names as found there and follows the signatures of those methods as far as they make sense (in the context of typed lists).

Some methods were added to make life a little easier (for typed lists):

func (l *List[T]) PopFront() T
func (l *List[T]) PopBack() T 

to get the first (rsp. last) value of a list and remove the corresponding element. Both methods will panic in case the List is empty.

func MakeList[T any](values ...T) *List[T]
func (l *List[T]) ToSlice() []T 

to create a list from a slice (or from single values) and to turn a list into a slice.

The method Value() was added to Element to actually retrieve a value from an element.

Iterators

Since version v2.0.3, Go style iterators are supported; i.e. we now have

lst := MakeList("a", "b")
for val := range lst.Iter() {
    fmt.Println(val)
}

which prints "a" and "b".

Previous Java style iterators are deprecated and will be removed in version 3.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

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

Element is a typed wrapper around list.Element

func (*Element[T]) Next

func (e *Element[T]) Next() *Element[T]

Next retrieves the next element from a list.

func (*Element[T]) Prev

func (e *Element[T]) Prev() *Element[T]

Prev retrievs the previous element from a list

func (*Element[T]) Value

func (e *Element[T]) Value() T

Value retrieves the value from a list (in a typed manner)

type Iterator

type Iterator[T any] interface {
	// ForEachRemaining calls a given function for each of the remaining elements
	ForEachRemaining(func(T))

	// HasNext returns true if there is an element to read from using Next
	HasNext() bool

	// Next returns the current element and proceeds the iterator to the next element
	Next() T

	// Remove removes the last returned element from the underlying list. Example:
	//
	//    iter := MakeList(1, 2, 3).Iterator()
	//    for iter.HasNext() {
	//        if iter.Next() == 2 {
	//            iter.Remove()
	//        }
	//    }
	//
	// leaves [1,3]
	Remove()
}

Iterator (deprecated) sets ground for a Java-Style iterator for lists. permit iteration.

Example:

iter := MakeList(1,2,3).Iterator()
for iter.HasNext() {
   fmt.Print(iter.Next())
}

type List

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

List is a typed wrapper around list.List

func MakeList

func MakeList[T any](values ...T) *List[T]

MakeList creates a list from a slice.

func New

func New[T any]() *List[T]

New Creates a new list

func (*List[T]) Back

func (l *List[T]) Back() *Element[T]

Back returns the last element from a list

func (*List[T]) Copy added in v2.0.2

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

Copy returns a copy of the current list.

func (*List[T]) EltAtIndex added in v2.0.3

func (l *List[T]) EltAtIndex(idx int) *Element[T]

EltAtIndex returns the element of a list at some index idx (or panics, if the list is too short). The first element has index 0.

Example

MakeList("a","b","c").EltAtIndex(1).Value()

Returns "b".

func (*List[T]) Front

func (l *List[T]) Front() *Element[T]

Front returns the first element from a list

func (*List[T]) Init

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

Init erases & initializes the current list.

func (*List[T]) InsertAfter

func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T]

InsertAfter inserts a new element with value v to a list after the element mark

func (*List[T]) InsertBefore

func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T]

InsertAfter inserts a new element with value v to a list before the element mark

func (*List[T]) Iter added in v2.0.3

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

Iter returns an iterator for the list. Example:

lst := MakeList("a", "b")
for i := range lst.Iter() {
     fmt.Println(i)
}

will print the strings "a" and "b".

func (*List[T]) Iterator

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

Iterator creates a Java style iterator for the given list. It should be used like:

iter := MakeList(1,2,3).Iterator()
for iter.HasNext() {
   fmt.Print(iter.Next())
}

func (*List[T]) Len

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

Len returns the length of a list

func (*List[T]) MoveAfter

func (l *List[T]) MoveAfter(e, mark *Element[T])

MoveAfter moves a given element e after another element mark. e and mark must not be nil.

func (*List[T]) MoveBefore

func (l *List[T]) MoveBefore(e, mark *Element[T])

MoveBefore moves a given element e before another element mark. e and mark must not be nil.

func (*List[T]) MoveToBack

func (l *List[T]) MoveToBack(e *Element[T])

MoveToBack moves e to the back of the list. If e is not an element of the list, nothing is changed

func (*List[T]) MoveToFront

func (l *List[T]) MoveToFront(e *Element[T])

MoveToBack moves e to the front of the list. If e is not an element of the list, nothing is changed

func (*List[T]) PopBack

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

PopBack Removes the last element of a list and returns its value. The list must not be empty.

func (*List[T]) PopFront

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

PopFront Removes the first element of a list and returns its value. The list must not be empty.

func (*List[T]) PushBack

func (l *List[T]) PushBack(v T) *Element[T]

PushBack creates a new element with value v at the end of the list and returns it.

func (*List[T]) PushBackList

func (l *List[T]) PushBackList(other *List[T])

PushBackList appends values of another list to a given list. The other list is not changed.

func (*List[T]) PushFront

func (l *List[T]) PushFront(v T) *Element[T]

PushFront creates a new element with value v at the start of the list and returns it.

func (*List[T]) PushFrontList

func (l *List[T]) PushFrontList(other *List[T])

PushFrontList puts values of another list to the front of a given list. The other list is not changed. The other list must not be nil.

func (*List[T]) Remove

func (l *List[T]) Remove(e *Element[T]) T

Remove removes an element from a list. The element must not be nil.

func (*List[T]) ToSlice

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

ToSlice returns a slice of the values of the elements of a list.

func (*List[T]) ValueAtIndex added in v2.0.4

func (l *List[T]) ValueAtIndex(idx int) T

ValueAtIndex returns the value of the element of a list at some index idx (or panics, if the list is too short). The first element has index 0.

Example

MakeList("a","b","c").ValueAtIndex(1)

Returns "b".

Jump to

Keyboard shortcuts

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