-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
74 lines (70 loc) · 1.53 KB
/
list.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package lazy
import (
"go4ml.xyz/errstr"
"reflect"
)
func List(list interface{}) Source {
return func(xs ...interface{}) Stream {
index, stride := 0, 1
for _, x := range xs {
if f, ok := x.(func() (int, int, Prefetch)); ok {
index, stride, _ = f()
} else {
return Error(errstr.Errorf("unsupported source option: %v", x))
}
}
v := reflect.ValueOf(list)
return func(next bool) (r interface{}, i int) {
if next && index < v.Len() {
r, i = v.Index(index).Interface(), index
index += stride
return
}
return EoS, index
}
}
}
func Sequence(gen func(int /*optional*/) interface{}) Source {
return func(xs ...interface{}) Stream {
pf := NoPrefetch
worker := 0
for _, x := range xs {
if f, ok := x.(func() (int, int, Prefetch)); ok {
worker, _, pf = f()
} else {
return Error(errstr.Errorf("unsupported source option: %v", x))
}
}
return pf(worker, func() Stream {
n := 0
return func(next bool) (v interface{}, i int) {
if next {
v, i = gen(n), n
n++
return
}
return EoS, n
}
})
}
}
func Generator(gen func(int) interface{}) Source {
return func(xs ...interface{}) Stream {
index, stride := 0, 1
for _, x := range xs {
if f, ok := x.(func() (int, int, Prefetch)); ok {
index, stride, _ = f()
} else {
return Error(errstr.Errorf("unsupported source option: %v", x))
}
}
return func(next bool) (v interface{}, i int) {
if next {
v, i = gen(index), index
index += stride
return
}
return EoS, index
}
}
}