lists

April 19, 2026 ยท View on GitHub

sink-logo

lists

lists provides a doubly linked list with a familiar API and container-style behavior.

It is designed to help you manage ordered collections with small functions for:

  • inserting and removing values at either end or around a given element
  • moving elements within the list
  • inspecting the list's contents and structure
  • copying values between lists

Overview

Use lists when you want linked list behavior that is easier to read, reuse, and test.

It is especially useful when:

  • you need stable insertion and removal at either end without shifting elements
  • you want to move elements within the collection without rebuilding it
  • a linked structure is a better fit than a slice

Core concepts

List

The List type represents the collection itself and manages the linked structure.

Element

An Element represents a node in the list and can be used to navigate forward and backward.

When to use it

Use lists when:

  • you need stable insertion and removal behavior at either end
  • you want to move elements without rebuilding the whole collection
  • a linked structure is a better fit than a slice

Prefer a slice when:

  • random access by index is required
  • the collection is small and insertion order does not matter
  • allocation simplicity is more important than insertion performance

API reference

Create or reset a list

MethodPurpose
NewCreates a new list from initial values
InitInitializes or resets the list
ClearRemoves all elements from the list

Inspect the list

MethodPurpose
LenReturns the number of elements
SizeAlias for Len
FrontReturns the first element
BackReturns the last element
IsEmptyReports whether the list is empty
ValuesReturns all values as a slice

Insert values

MethodPurpose
PushFrontInserts a value at the front
PrependInserts multiple values at the front
PushBackInserts a value at the back
AppendInserts multiple values at the back
InsertBeforeInserts a value before a mark element
InsertAfterInserts a value after a mark element
PushBackListAppends all values from another list
PushFrontListPrepends all values from another list

Move or remove elements

MethodPurpose
RemoveRemoves an element and returns its value
MoveToFrontMoves an element to the front
MoveToBackMoves an element to the back
MoveBeforeMoves an element before another element
MoveAfterMoves an element after another element

Visit elements

MethodPurpose
ForEachCalls a function on each element in order
MethodPurpose
NextReturns the next element in the list, or nil if at the end
PrevReturns the previous element in the list, or nil if at the start

Notes

  • Prefer the method that most clearly expresses your intent.
  • Elements are only valid within the list they belong to.
  • Mutating a list may affect element references.
  • Use the list's own methods to manage membership and movement.

Examples

Examples can be found in the test suite.