elvish/eval/builtin_fn_flow.go

124 lines
2.5 KiB
Go
Raw Normal View History

2017-12-17 13:20:03 +08:00
package eval
import (
"errors"
"sync"
)
// Flow control.
func init() {
2018-02-07 03:39:40 +08:00
addBuiltinFns(map[string]interface{}{
"run-parallel": runParallel,
// Exception and control
"fail": fail,
"multi-error": multiErrorFn,
"return": returnFn,
"break": breakFn,
"continue": continueFn,
2017-12-17 13:20:03 +08:00
// Iterations.
2018-02-05 15:35:49 +08:00
"each": each,
"peach": peach,
2017-12-17 13:20:03 +08:00
})
}
func runParallel(fm *Frame, functions ...Callable) error {
2017-12-17 13:20:03 +08:00
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 := fm2.PCall(function, NoArgs, NoOpts)
2017-12-17 13:20:03 +08:00
if err != nil {
*exception = err.(*Exception)
}
waitg.Done()
}(fm.fork("[run-parallel function]"), function, &exceptions[i])
2017-12-17 13:20:03 +08:00
}
waitg.Wait()
return ComposeExceptionsFromPipeline(exceptions)
2017-12-17 13:20:03 +08:00
}
// each takes a single closure and applies it to all input values.
2018-02-05 15:35:49 +08:00
func each(fm *Frame, f Callable, inputs Inputs) {
2017-12-17 13:20:03 +08:00
broken := false
2018-02-05 15:35:49 +08:00
inputs(func(v interface{}) {
2017-12-17 13:20:03 +08:00
if broken {
return
}
// NOTE We don't have the position range of the closure in the source.
// Ideally, it should be kept in the Closure itself.
2018-02-05 15:35:49 +08:00
newec := fm.fork("closure of each")
2017-12-17 13:20:03 +08:00
newec.ports[0] = DevNullClosedChan
ex := newec.PCall(f, []interface{}{v}, NoOpts)
2017-12-17 13:20:03 +08:00
ClosePorts(newec.ports)
if ex != nil {
switch ex.(*Exception).Cause {
case nil, Continue:
// nop
case Break:
broken = true
default:
throw(ex)
}
}
})
}
// peach takes a single closure and applies it to all input values in parallel.
2018-02-05 15:35:49 +08:00
func peach(fm *Frame, f Callable, inputs Inputs) {
2017-12-17 13:20:03 +08:00
var w sync.WaitGroup
broken := false
var err error
2018-02-05 15:35:49 +08:00
inputs(func(v interface{}) {
2017-12-17 13:20:03 +08:00
if broken || err != nil {
return
}
w.Add(1)
go func() {
// NOTE We don't have the position range of the closure in the source.
// Ideally, it should be kept in the Closure itself.
2018-02-05 15:35:49 +08:00
newec := fm.fork("closure of peach")
2017-12-17 13:20:03 +08:00
newec.ports[0] = DevNullClosedChan
ex := newec.PCall(f, []interface{}{v}, NoOpts)
2017-12-17 13:20:03 +08:00
ClosePorts(newec.ports)
if ex != nil {
switch ex.(*Exception).Cause {
case nil, Continue:
// nop
case Break:
broken = true
default:
err = ex
}
}
w.Done()
}()
})
w.Wait()
maybeThrow(err)
}
func fail(msg string) error {
return errors.New(msg)
2017-12-17 13:20:03 +08:00
}
func multiErrorFn(excs ...*Exception) error {
return PipelineError{excs}
2017-12-17 13:20:03 +08:00
}
func returnFn() error {
return Return
2017-12-17 13:20:03 +08:00
}
func breakFn() error {
return Break
2017-12-17 13:20:03 +08:00
}
func continueFn() error {
return Continue
2017-12-17 13:20:03 +08:00
}