40 lines
603 B
Go
40 lines
603 B
Go
package iterator
|
|
|
|
import "fmt"
|
|
|
|
type Iterator[T any] struct {
|
|
data []T
|
|
index int
|
|
}
|
|
|
|
func New[T any](items []T) Iterator[T] {
|
|
return Iterator[T]{ data: items, index: 0 }
|
|
}
|
|
|
|
func (i Iterator[T]) Index() int {
|
|
return i.index
|
|
}
|
|
|
|
func (i Iterator[T]) IsDone() bool {
|
|
return i.index == len(i.data)
|
|
}
|
|
|
|
func (i Iterator[T]) Peek() (T, error) {
|
|
var null T
|
|
|
|
if i.IsDone() {
|
|
return null, fmt.Errorf("Iterator is exhausted.")
|
|
}
|
|
|
|
return i.data[i.index], nil
|
|
}
|
|
|
|
func (i *Iterator[T]) Next() (T, error) {
|
|
if val, err := i.Peek(); err != nil {
|
|
return val, err
|
|
} else {
|
|
i.index++
|
|
return val, nil
|
|
}
|
|
}
|