feat: cleaner parsing functions
This commit is contained in:
@@ -48,6 +48,12 @@ func (i Iterator[T]) MustGet() T {
|
||||
return i.items[i.index]
|
||||
}
|
||||
|
||||
func (i *Iterator[T]) Forward() {
|
||||
if !i.Done() {
|
||||
i.index++
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new iterator, over a set of items.
|
||||
func (i *Iterator[T]) Next() (T, error) {
|
||||
item, err := i.Get()
|
||||
@@ -67,3 +73,14 @@ func (i *Iterator[T]) Back() {
|
||||
func (i Iterator[T]) Done() bool {
|
||||
return i.index == len(i.items)
|
||||
}
|
||||
|
||||
func Do[T any, U any](i *Iterator[T], fn func(i *Iterator[T]) (U, error)) (U, error) {
|
||||
i2 := i.Copy()
|
||||
|
||||
out, err := fn(i2)
|
||||
if err == nil {
|
||||
i.Sync(i2)
|
||||
}
|
||||
|
||||
return out, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user