1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/collections/slice.go

45 lines
876 B
Go
Raw Normal View History

package collections
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type SliceIterator struct {
valVar string
keyVar string
values []core.Value
pos int
}
func NewSliceIterator(
valVar,
keyVar string,
input []core.Value,
) Iterator {
return &SliceIterator{valVar, keyVar, input, 0}
}
func NewDefaultSliceIterator(input []core.Value) Iterator {
return NewSliceIterator(DefaultValueVar, DefaultKeyVar, input)
}
func (iterator *SliceIterator) HasNext() bool {
return len(iterator.values) > iterator.pos
}
func (iterator *SliceIterator) Next() (DataSet, error) {
if len(iterator.values) > iterator.pos {
idx := iterator.pos
val := iterator.values[idx]
iterator.pos++
return DataSet{
iterator.valVar: val,
iterator.keyVar: values.NewInt(idx),
}, nil
}
return nil, ErrExhausted
}