-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexecutable.go
More file actions
59 lines (48 loc) · 1.23 KB
/
executable.go
File metadata and controls
59 lines (48 loc) · 1.23 KB
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
package co
import (
"log"
)
type executable[R any] struct {
fn func() (R, error)
}
func NewExecutor[R any]() *executable[R] {
return &executable[R]{}
}
func (e *executable[R]) exe() (R, error) {
defer func() {
if r := recover(); r != nil {
log.Println("executable function error: %w", r)
}
}()
return e.fn()
}
type executablesList[R any] struct {
*iterativeList[*executable[R]]
}
func NewExecutablesList[R any]() *executablesList[R] {
return &executablesList[R]{
iterativeList: NewIterativeList[*executable[R]](),
}
}
func (c *executablesList[R]) AddExecutable(fns ...func() (R, error)) *executablesList[R] {
for i := range fns {
e := NewExecutor[R]()
e.fn = fns[i]
c.add(e)
}
return c
}
func (it *executablesList[R]) iterator() executableListIterator[R] {
return executableListIterator[R]{iterativeListIterator: it.iterativeList.iterator(), executablesList: it}
}
type executableListIterator[R any] struct {
*executablesList[R]
*iterativeListIterator[*executable[R]]
}
func (it *executableListIterator[R]) preflight() bool {
defer func() { it.currentIndex++ }()
return it.iterativeListIterator.preflight()
}
func (it *executableListIterator[R]) exeAt(i int) (R, error) {
return it.executablesList.getAt(i).exe()
}