mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-16 11:37:36 +02:00
35 lines
610 B
Go
35 lines
610 B
Go
package collections
|
|
|
|
import (
|
|
"context"
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
)
|
|
|
|
type (
|
|
Iterator interface {
|
|
Next(ctx context.Context, scope *core.Scope) (*core.Scope, error)
|
|
}
|
|
|
|
Iterable interface {
|
|
Iterate(ctx context.Context, scope *core.Scope) (Iterator, error)
|
|
}
|
|
)
|
|
|
|
func ToSlice(ctx context.Context, scope *core.Scope, iterator Iterator) ([]*core.Scope, error) {
|
|
res := make([]*core.Scope, 0, 10)
|
|
|
|
for {
|
|
nextScope, err := iterator.Next(ctx, scope.Fork())
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if nextScope == nil {
|
|
return res, nil
|
|
}
|
|
|
|
res = append(res, nextScope)
|
|
}
|
|
}
|