Debian Codegraph @

list/list.go

raw symbols SCIP
1 package list
2
3 import "github.com/jesseduffield/generics/slices"
4
5 // List is a struct which wraps a slice and provides convenience methods for it.
6 // Unfortunately due to some limitations in Go's type system, certain methods
7 // are not available e.g. Map.
8
9 type List[T any] struct {
10 slice []T
11 }
12
13 func New[T any]() *List[T] {
14 return &List[T]{}
15 }
16
17 func NewFromSlice[T any](slice []T) *List[T] {
18 return &List[T]{slice: slice}
19 }
20
21 func (l *List[T]) ToSlice() []T {
22 return l.slice
23 }
24
25 // Mutative methods
26
27 func (l *List[T]) Push(v T) {
28 l.slice = append(l.slice, v)
29 }
30
31 func (l *List[T]) Pop() T {
32 var value T
33 value, l.slice = slices.Pop(l.slice)
34 return value
35 }
36
37 func (l *List[T]) Insert(index int, values ...T) {
38 l.slice = slices.Insert(l.slice, index, values...)
39 }
40
41 func (l *List[T]) Append(values ...T) {
42 l.slice = append(l.slice, values...)
43 }
44
45 func (l *List[T]) Prepend(values ...T) {
46 l.slice = append(values, l.slice...)
47 }
48
49 func (l *List[T]) Remove(index int) {
50 l.Delete(index, index+1)
51 }
52
53 func (l *List[T]) Delete(from int, to int) {
54 l.slice = slices.Delete(l.slice, from, to)
55 }
56
57 func (l *List[T]) FilterInPlace(test func(value T) bool) {
58 l.slice = slices.FilterInPlace(l.slice, test)
59 }
60
61 func (l *List[T]) MapInPlace(f func(value T) T) {
62 slices.MapInPlace(l.slice, f)
63 }
64
65 func (l *List[T]) ReverseInPlace() {
66 slices.ReverseInPlace(l.slice)
67 }
68
69 // Non-mutative methods
70
71 // Similar to Append but we leave the original slice untouched and return a new list
72 func (l *List[T]) Concat(values ...T) *List[T] {
73 return NewFromSlice(slices.Concat(l.slice, values...))
74 }
75
76 func (l *List[T]) Filter(test func(value T) bool) *List[T] {
77 return NewFromSlice(slices.Filter(l.slice, test))
78 }
79
80 // Unfortunately this does not support mapping from one type to another
81 // because Go does not yet (and may never) support methods defining their own
82 // type parameters. For that functionality you'll need to use the standalone
83 // Map function instead
84 func (l *List[T]) Map(f func(value T) T) *List[T] {
85 return NewFromSlice(slices.Map(l.slice, f))
86 }
87
88 func (l *List[T]) Clone() *List[T] {
89 return NewFromSlice(slices.Clone(l.slice))
90 }
91
92 func (l *List[T]) Some(test func(value T) bool) bool {
93 return slices.Some(l.slice, test)
94 }
95
96 func (l *List[T]) Every(test func(value T) bool) bool {
97 return slices.Every(l.slice, test)
98 }
99
100 func (l *List[T]) IndexFunc(f func(T) bool) int {
101 return slices.IndexFunc(l.slice, f)
102 }
103
104 func (l *List[T]) ContainsFunc(f func(T) bool) bool {
105 return slices.ContainsFunc(l.slice, f)
106 }
107
108 func (l *List[T]) Reverse() *List[T] {
109 return NewFromSlice(slices.Reverse(l.slice))
110 }
111
112 func (l *List[T]) IsEmpty() bool {
113 return len(l.slice) == 0
114 }
115
116 func (l *List[T]) Len() int {
117 return len(l.slice)
118 }
119
120 func (l *List[T]) Get(index int) T {
121 return l.slice[index]
122 }