Documentation
¶
Overview ¶
Package list provides a type-safe wrapper to the standard container/list
Index ¶
- type List
- func (l *List[T]) Back() (T, bool)
- func (l *List[T]) Clone() *List[T]
- func (l *List[T]) Copy(fn func(T) (T, bool)) *List[T]
- func (l *List[T]) DeleteMatchFn(fn func(T) bool)
- func (l *List[T]) FirstMatchFn(fn func(T) bool) (T, bool)
- func (l *List[T]) ForEach(fn func(T) bool)
- func (l *List[T]) Front() (T, bool)
- func (l *List[T]) Len() int
- func (l *List[T]) MoveToBackFirstMatchFn(fn func(T) bool)
- func (l *List[T]) MoveToFrontFirstMatchFn(fn func(T) bool)
- func (l *List[T]) PopFirstMatchFn(fn func(T) bool) (T, bool)
- func (l *List[T]) Purge() int
- func (l *List[T]) PushBack(v T)
- func (l *List[T]) PushFront(v T)
- func (l *List[T]) Sys() *list.List
- func (l *List[T]) Values() []T
- func (*List[T]) Zero() T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type List ¶
List is a typed wrapper on top of list.List.
func New ¶ added in v0.2.2
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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]) MoveToBackFirstMatchFn ¶ added in v0.2.2
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
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 ¶
PopFirstMatchFn removes and returns the first match, iterating from front to back.
func (*List[T]) Purge ¶
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]