-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.go
More file actions
61 lines (51 loc) · 1.42 KB
/
slice.go
File metadata and controls
61 lines (51 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package enumerators
// SliceEnumerator provides enumeration over a Go slice.
type SliceEnumerator[T any] struct {
slice []T
cursor int
current T
err error
}
// MoveNext advances the enumerator to the next element in the slice.
// Returns true if more elements are available, false otherwise.
func (e *SliceEnumerator[T]) MoveNext() bool {
e.cursor++
if e.cursor >= len(e.slice) {
return false
}
e.current = e.slice[e.cursor]
return true
}
// Current returns the current element and any error encountered.
func (e *SliceEnumerator[T]) Current() (T, error) {
return e.current, e.err
}
// Err returns any error encountered during enumeration.
func (e *SliceEnumerator[T]) Err() error {
return e.err
}
// Dispose cleans up resources. For SliceEnumerator, this is a no-op.
func (e *SliceEnumerator[T]) Dispose() {
// no-op
}
// Slice creates a new enumerator that iterates over the provided slice.
func Slice[T any](slice []T) Enumerator[T] {
return &SliceEnumerator[T]{
slice: slice,
cursor: -1,
}
}
// ToSlice converts an enumerator to a slice by consuming all its elements.
// The enumerator is automatically disposed after consumption.
func ToSlice[T any](enumerator Enumerator[T]) ([]T, error) {
defer enumerator.Dispose()
var slice []T
for enumerator.MoveNext() {
item, err := enumerator.Current()
if err != nil {
return slice, err
}
slice = append(slice, item)
}
return slice, enumerator.Err()
}