-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
63 lines (51 loc) · 1.37 KB
/
runner.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
// Copyright (C) 2022-2023 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
package sabi
import (
"github.com/sttk/sabi/errs"
)
type /* error reasons */ (
// FailToRunInParallel is an error reason which indicates that some of runner
// functions running in parallel failed.
FailToRunInParallel struct {
Errors map[int]errs.Err
}
)
// Seq is the function which runs argument functions sequencially.
func Seq(runners ...func() errs.Err) errs.Err {
for _, runner := range runners {
err := runner()
if err.IsNotOk() {
return err
}
}
return errs.Ok()
}
// Seq_ is the function which creates a runner function which runs Seq
// function.
func Seq_(runners ...func() errs.Err) func() errs.Err {
return func() errs.Err {
return Seq(runners...)
}
}
// Para is the function which runs argument functions in parallel.
func Para(runners ...func() errs.Err) errs.Err {
var ag asyncGroupAsync[int]
for i, runner := range runners {
ag.name = i
ag.Add(runner)
}
ag.wait()
if ag.hasErr() {
return errs.New(FailToRunInParallel{Errors: ag.makeErrs()})
}
return errs.Ok()
}
// Para_ is the function which creates a runner function which runs Para
// function.
func Para_(runners ...func() errs.Err) func() errs.Err {
return func() errs.Err {
return Para(runners...)
}
}