-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
42 lines (39 loc) · 859 Bytes
/
utils.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
package lattice
func Do(run func(int, DataType) (NodeIterator, error), support int, dt DataType, do func(Node) error) error {
it, err := run(support, dt)
if err != nil {
return err
}
var node Node
for node, err, it = it(); it != nil; node, err, it = it() {
e := do(node)
if e != nil {
return e
}
}
return err
}
func Slice(run func(int, DataType) (NodeIterator, error), support int, dt DataType) ([]Node, error) {
nodes := make([]Node, 0, 10)
err := Do(run, support, dt,
func(n Node) error {
nodes = append(nodes, n)
return nil
})
if err != nil {
return nil, err
}
return nodes, nil
}
func NodeIteratorFromSlice(nodes []Node) (it NodeIterator, err error) {
i := 0
it = func() (Node, error, NodeIterator) {
if i >= len(nodes) {
return nil, nil, nil
}
n := nodes[i]
i++
return n, nil, it
}
return it, nil
}