-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
builtin_fn_flow.go
263 lines (236 loc) · 5.45 KB
/
builtin_fn_flow.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package eval
import (
"sync"
"github.com/elves/elvish/pkg/eval/vals"
"github.com/elves/elvish/pkg/util"
)
// Flow control.
//elvdoc:fn run-parallel
//
// ```elvish
// run-parallel $callable ...
// ```
//
// Run several callables in parallel, and wait for all of them to finish.
//
// If one or more callables throw exceptions, the other callables continue running,
// and a composite exception is thrown when all callables finish execution.
//
// The behavior of `run-parallel` is consistent with the behavior of pipelines,
// except that it does not perform any redirections.
//
// Here is an example that lets you pipe the stdout and stderr of a command to two
// different commands:
//
// ```elvish
// pout = (pipe)
// perr = (pipe)
// run-parallel {
// foo > $pout 2> $perr
// pwclose $pout
// pwclose $perr
// } {
// bar < $pout
// prclose $pout
// } {
// bar2 < $perr
// prclose $perr
// }
// ```
//
// This command is intended for doing a fixed number of heterogeneous things in
// parallel. If you need homogeneous parallel processing of possibly unbound data,
// use `peach` instead.
//
// @cf peach
// TODO(xiaq): Document "multi-error".
// TODO(xiaq): Document "return", "break" and "continue".
//elvdoc:fn each
//
// ```elvish
// each $f $input-list?
// ```
//
// Call `$f` on all inputs. Examples:
//
// ```elvish-transcript
// ~> range 5 8 | each [x]{ ^ $x 2 }
// ▶ 25
// ▶ 36
// ▶ 49
// ~> each [x]{ put $x[:3] } [lorem ipsum]
// ▶ lor
// ▶ ips
// ```
//
// @cf peach
//
// Etymology: Various languages, as `for each`. Happens to have the same name as
// the iteration construct of
// [Factor](http://docs.factorcode.org/content/word-each,sequences.html).
//elvdoc:fn peach
//
// ```elvish
// peach $f $input-list?
// ```
//
// Call `$f` on all inputs, possibly in parallel.
//
// Example (your output will differ):
//
// ```elvish-transcript
// ~> range 1 7 | peach [x]{ + $x 10 }
// ▶ 12
// ▶ 11
// ▶ 13
// ▶ 16
// ▶ 15
// ▶ 14
// ```
//
// This command is intended for homogeneous processing of possibly unbound data. If
// you need to do a fixed number of heterogeneous things in parallel, use
// `run-parallel`.
//
// @cf each run-parallel
func init() {
addBuiltinFns(map[string]interface{}{
"run-parallel": runParallel,
// Exception and control
"fail": fail,
"multi-error": multiErrorFn,
"return": returnFn,
"break": breakFn,
"continue": continueFn,
// Iterations.
"each": each,
"peach": peach,
})
}
func runParallel(fm *Frame, functions ...Callable) error {
var waitg sync.WaitGroup
waitg.Add(len(functions))
exceptions := make([]*Exception, len(functions))
for i, function := range functions {
go func(fm2 *Frame, function Callable, exception **Exception) {
err := function.Call(fm2, NoArgs, NoOpts)
if err != nil {
*exception = err.(*Exception)
}
waitg.Done()
}(fm.fork("[run-parallel function]"), function, &exceptions[i])
}
waitg.Wait()
return makePipelineError(exceptions)
}
// each takes a single closure and applies it to all input values.
func each(fm *Frame, f Callable, inputs Inputs) error {
broken := false
var err error
inputs(func(v interface{}) {
if broken {
return
}
newFm := fm.fork("closure of each")
ex := f.Call(newFm, []interface{}{v}, NoOpts)
newFm.Close()
if ex != nil {
switch Cause(ex) {
case nil, Continue:
// nop
case Break:
broken = true
default:
broken = true
err = ex
}
}
})
return err
}
// peach takes a single closure and applies it to all input values in parallel.
func peach(fm *Frame, f Callable, inputs Inputs) error {
var w sync.WaitGroup
broken := false
var err error
inputs(func(v interface{}) {
if broken || err != nil {
return
}
w.Add(1)
go func() {
newFm := fm.fork("closure of peach")
newFm.ports[0] = DevNullClosedChan
ex := f.Call(newFm, []interface{}{v}, NoOpts)
newFm.Close()
if ex != nil {
switch Cause(ex) {
case nil, Continue:
// nop
case Break:
broken = true
default:
broken = true
err = util.Errors(err, ex)
}
}
w.Done()
}()
})
w.Wait()
return err
}
// FailError is an error returned by the "fail" command.
type FailError struct{ Content interface{} }
// Error returns the string representation of the cause.
func (e FailError) Error() string { return vals.ToString(e.Content) }
// Fields returns a structmap for accessing fields from Elvish.
func (e FailError) Fields() vals.StructMap { return failFields{e} }
type failFields struct{ e FailError }
func (failFields) IsStructMap() {}
func (f failFields) Type() string { return "fail" }
func (f failFields) Content() interface{} { return f.e.Content }
//elvdoc:fn fail
//
// ```elvish
// fail $v
// ```
//
// Throws an exception; `$v` may be any type. If `$v` is already an exception,
// `fail` rethrows it.
//
// ```elvish-transcript
// ~> fail bad
// Exception: bad
// [tty 9], line 1: fail bad
// ~> put ?(fail bad)
// ▶ ?(fail bad)
// ~> fn f { fail bad }
// ~> fail ?(f)
// Exception: bad
// Traceback:
// [tty 7], line 1:
// fn f { fail bad }
// [tty 8], line 1:
// fail ?(f)
// ```
func fail(v interface{}) error {
if e, ok := v.(error); ok {
// MAYBE TODO: if v is an exception, attach a "rethrown" stack trace,
// like Java
return e
}
return FailError{v}
}
func multiErrorFn(excs ...*Exception) error {
return PipelineError{excs}
}
func returnFn() error {
return Return
}
func breakFn() error {
return Break
}
func continueFn() error {
return Continue
}