-
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathiter.go
More file actions
49 lines (41 loc) · 790 Bytes
/
iter.go
File metadata and controls
49 lines (41 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gojq
// Iter is an interface for an iterator.
type Iter interface {
Next() (any, bool)
}
// NewIter creates a new [Iter] from values.
func NewIter[T any](values ...T) Iter {
switch len(values) {
case 0:
return emptyIter{}
case 1:
return &unitIter{value: values[0]}
default:
iter := sliceIter[T](values)
return &iter
}
}
type emptyIter struct{}
func (emptyIter) Next() (any, bool) {
return nil, false
}
type unitIter struct {
value any
done bool
}
func (iter *unitIter) Next() (any, bool) {
if iter.done {
return nil, false
}
iter.done = true
return iter.value, true
}
type sliceIter[T any] []T
func (iter *sliceIter[T]) Next() (any, bool) {
if len(*iter) == 0 {
return nil, false
}
value := (*iter)[0]
*iter = (*iter)[1:]
return value, true
}