-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.go
More file actions
46 lines (40 loc) · 1.23 KB
/
range.go
File metadata and controls
46 lines (40 loc) · 1.23 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
package enumerators
// rangeEnumerator generates a sequence of values using a factory function.
type rangeEnumerator[T any] struct {
start int
end int
current T
err error
factory func(int) T
}
// MoveNext advances the enumerator to the next value in the range.
// It returns true if there are more values to enumerate, and false otherwise.
func (e *rangeEnumerator[T]) MoveNext() bool {
if e.start < e.end {
e.current = e.factory(e.start)
e.start++
return true
}
return false
}
// Current returns the current value from the enumerator.
func (e *rangeEnumerator[T]) Current() (T, error) {
return e.current, e.err
}
// Err returns any error encountered during enumeration.
func (e *rangeEnumerator[T]) Err() error {
return e.err
}
// Dispose cleans up resources. For rangeEnumerator, this is a no-op.
func (e *rangeEnumerator[T]) Dispose() {
// no-op
}
// Range creates an enumerator that generates a sequence of values using a factory function.
// It starts at 'seed' and generates 'count' values by calling factory(i) for i from seed to seed+count-1.
func Range[T any](seed int, count int, factory func(i int) T) Enumerator[T] {
return &rangeEnumerator[T]{
start: seed,
end: seed + count,
factory: factory,
}
}