list

package
v0.3.1 Latest Latest
Warning

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

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

Documentation

Overview

Package list provides a type-safe wrapper to the standard container/list

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List[T any] list.List

List is a typed wrapper on top of list.List.

func New added in v0.2.2

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

New creates a new List with optional initial entries. It returns a pointer to the newly created List.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	// Create a new list with initial values
	l := list.New(1, 2, 3)
	fmt.Println("Length:", l.Len())
	fmt.Println("Values:", l.Values())
}
Output:

Length: 3
Values: [1 2 3]

func (*List[T]) Back

func (l *List[T]) Back() (T, bool)

Back returns the last value in the list.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New("first", "second", "third")
	if v, ok := l.Back(); ok {
		fmt.Println("Back:", v)
	}
}
Output:

Back: third

func (*List[T]) Clone

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

Clone returns a shallow copy of the list.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New("a", "b", "c")
	cloned := l.Clone()
	cloned.PushBack("d")
	fmt.Println("Original:", l.Values())
	fmt.Println("Cloned:", cloned.Values())
}
Output:

Original: [a b c]
Cloned: [a b c d]

func (*List[T]) Copy

func (l *List[T]) Copy(fn func(T) (T, bool)) *List[T]

Copy returns a copy of the list, optionally altered or filtered.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New(1, 2, 3, 4, 5)
	// Copy only even numbers, doubled
	copied := l.Copy(func(v int) (int, bool) {
		if v%2 == 0 {
			return v * 2, true
		}
		return 0, false
	})
	fmt.Println("Original:", l.Values())
	fmt.Println("Copied:", copied.Values())
}
Output:

Original: [1 2 3 4 5]
Copied: [4 8]

func (*List[T]) DeleteMatchFn

func (l *List[T]) DeleteMatchFn(fn func(T) bool)

DeleteMatchFn deletes elements on the list satisfying the given function.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New(1, 2, 3, 4, 5)
	// Delete even numbers
	l.DeleteMatchFn(func(v int) bool {
		return v%2 == 0
	})
	fmt.Println("After deletion:", l.Values())
}
Output:

After deletion: [1 3 5]

func (*List[T]) FirstMatchFn

func (l *List[T]) FirstMatchFn(fn func(T) bool) (T, bool)

FirstMatchFn returns the first element that satisfies the given function from the front to the back.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New(1, 2, 3, 4, 5)
	// Find first even number
	if v, ok := l.FirstMatchFn(func(n int) bool {
		return n%2 == 0
	}); ok {
		fmt.Println("First even:", v)
	}
}
Output:

First even: 2

func (*List[T]) ForEach

func (l *List[T]) ForEach(fn func(T) bool)

ForEach calls a function on each element of the list, allowing safe modification of the list during iteration.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New(1, 2, 3, 4, 5)
	sum := 0
	l.ForEach(func(v int) bool {
		sum += v
		return true // continue iteration
	})
	fmt.Println("Sum:", sum)
}
Output:

Sum: 15
Example (EarlyTermination)
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New(1, 2, 3, 4, 5)
	var collected []int
	l.ForEach(func(v int) bool {
		collected = append(collected, v)
		return v < 3 // stop when v >= 3
	})
	fmt.Println("Collected:", collected)
}
Output:

Collected: [1 2 3]

func (*List[T]) Front

func (l *List[T]) Front() (T, bool)

Front returns the first value in the list.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New("first", "second", "third")
	if v, ok := l.Front(); ok {
		fmt.Println("Front:", v)
	}
}
Output:

Front: first

func (*List[T]) Len

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

Len returns the number of elements in the list

func (*List[T]) MoveToBackFirstMatchFn added in v0.2.2

func (l *List[T]) MoveToBackFirstMatchFn(fn func(T) bool)

MoveToBackFirstMatchFn moves the first element that satisfies the given function to the back of the list, searching from front to back.

func (*List[T]) MoveToFrontFirstMatchFn added in v0.2.2

func (l *List[T]) MoveToFrontFirstMatchFn(fn func(T) bool)

MoveToFrontFirstMatchFn moves the first element that satisfies the given function to the front of the list, searching from front to back.

func (*List[T]) PopFirstMatchFn

func (l *List[T]) PopFirstMatchFn(fn func(T) bool) (T, bool)

PopFirstMatchFn removes and returns the first match, iterating from front to back.

func (*List[T]) Purge

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

Purge removes any element not complying with the type restriction. It returns the number of elements removed.

func (*List[T]) PushBack

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

PushBack adds a value at the end of the list.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New[int]()
	l.PushBack(1)
	l.PushBack(2)
	l.PushBack(3)
	fmt.Println(l.Values())
}
Output:

[1 2 3]

func (*List[T]) PushFront

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

PushFront adds a value at the beginning of the list.

Example
package main

import (
	"fmt"

	"darvaza.org/x/container/list"
)

func main() {
	l := list.New[string]()
	l.PushBack("world")
	l.PushFront("hello")
	fmt.Println(l.Values())
}
Output:

[hello world]

func (*List[T]) Sys

func (l *List[T]) Sys() *list.List

Sys returns the native list.List

func (*List[T]) Values

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

Values returns all values in the list.

func (*List[T]) Zero

func (*List[T]) Zero() T

Zero returns the zero value of the type associated to the list.

Jump to

Keyboard shortcuts

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