1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/runtime/collections/slice.go
Tim Voronov 549b4abd3b
Feature/#5 collect keyword alt (#141)
Implemented COLLECT key word
2018-10-24 21:30:05 -04:00

45 lines
876 B
Go

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
}