Debian Codegraph @

tw/slicer.go

raw
1 package tw
2
3 import "slices"
4
5 // Slicer is a generic slice type that provides additional methods for slice manipulation.
6 type Slicer[T any] []T
7
8 // NewSlicer creates and returns a new initialized Slicer.
9 func NewSlicer[T any]() Slicer[T] {
10 return make(Slicer[T], 0)
11 }
12
13 // Get returns the element at the specified index.
14 // Returns the zero value if the index is out of bounds or the slice is nil.
15 func (s Slicer[T]) Get(index int) T {
16 if s == nil || index < 0 || index >= len(s) {
17 var zero T
18 return zero
19 }
20 return s[index]
21 }
22
23 // GetOK returns the element at the specified index and a boolean indicating whether the index was valid.
24 func (s Slicer[T]) GetOK(index int) (T, bool) {
25 if s == nil || index < 0 || index >= len(s) {
26 var zero T
27 return zero, false
28 }
29 return s[index], true
30 }
31
32 // Append appends elements to the slice and returns the new slice.
33 func (s Slicer[T]) Append(elements ...T) Slicer[T] {
34 return append(s, elements...)
35 }
36
37 // Prepend adds elements to the beginning of the slice and returns the new slice.
38 func (s Slicer[T]) Prepend(elements ...T) Slicer[T] {
39 return append(elements, s...)
40 }
41
42 // Len returns the number of elements in the slice.
43 // Returns 0 if the slice is nil.
44 func (s Slicer[T]) Len() int {
45 if s == nil {
46 return 0
47 }
48 return len(s)
49 }
50
51 // IsEmpty returns true if the slice is nil or has zero elements.
52 func (s Slicer[T]) IsEmpty() bool {
53 return s.Len() == 0
54 }
55
56 // Has returns true if the index exists in the slice.
57 func (s Slicer[T]) Has(index int) bool {
58 return index >= 0 && index < s.Len()
59 }
60
61 // First returns the first element of the slice, or the zero value if empty.
62 func (s Slicer[T]) First() T {
63 return s.Get(0)
64 }
65
66 // Last returns the last element of the slice, or the zero value if empty.
67 func (s Slicer[T]) Last() T {
68 return s.Get(s.Len() - 1)
69 }
70
71 // Each iterates over each element in the slice and calls the provided function.
72 // Does nothing if the slice is nil.
73 func (s Slicer[T]) Each(fn func(T)) {
74 for _, v := range s {
75 fn(v)
76 }
77 }
78
79 // Filter returns a new Slicer containing only elements that satisfy the predicate.
80 func (s Slicer[T]) Filter(fn func(T) bool) Slicer[T] {
81 result := NewSlicer[T]()
82 for _, v := range s {
83 if fn(v) {
84 result = result.Append(v)
85 }
86 }
87 return result
88 }
89
90 // Map returns a new Slicer with each element transformed by the provided function.
91 func (s Slicer[T]) Map(fn func(T) T) Slicer[T] {
92 result := NewSlicer[T]()
93 for _, v := range s {
94 result = result.Append(fn(v))
95 }
96 return result
97 }
98
99 // Contains returns true if the slice contains an element that satisfies the predicate.
100 func (s Slicer[T]) Contains(fn func(T) bool) bool {
101 return slices.ContainsFunc(s, fn)
102 }
103
104 // Find returns the first element that satisfies the predicate, along with a boolean indicating if it was found.
105 func (s Slicer[T]) Find(fn func(T) bool) (T, bool) {
106 for _, v := range s {
107 if fn(v) {
108 return v, true
109 }
110 }
111 var zero T
112 return zero, false
113 }
114
115 // Clone returns a shallow copy of the Slicer.
116 func (s Slicer[T]) Clone() Slicer[T] {
117 result := NewSlicer[T]()
118 if s != nil {
119 result = append(result, s...)
120 }
121 return result
122 }
123
124 // SlicerToMapper converts a Slicer of KeyValuePair to a Mapper.
125 func SlicerToMapper[K comparable, V any](s Slicer[KeyValuePair[K, V]]) Mapper[K, V] {
126 result := make(Mapper[K, V])
127 if s == nil {
128 return result
129 }
130 for _, pair := range s {
131 result[pair.Key] = pair.Value
132 }
133 return result
134 }