2021-12-26 20:18:28 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
2023-05-19 06:18:29 +08:00
|
|
|
"errors"
|
2021-12-26 20:18:28 +08:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"src.elv.sh/pkg/eval/errs"
|
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Stream manipulation.
|
|
|
|
|
|
|
|
func init() {
|
2022-03-20 23:50:25 +08:00
|
|
|
addBuiltinFns(map[string]any{
|
2021-12-26 20:18:28 +08:00
|
|
|
"all": all,
|
|
|
|
"one": one,
|
|
|
|
|
2022-08-29 05:57:37 +08:00
|
|
|
"take": take,
|
|
|
|
"drop": drop,
|
|
|
|
"compact": compact,
|
2021-12-26 20:18:28 +08:00
|
|
|
|
|
|
|
"count": count,
|
|
|
|
|
|
|
|
"order": order,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func all(fm *Frame, inputs Inputs) error {
|
|
|
|
out := fm.ValueOutput()
|
|
|
|
var errOut error
|
2022-03-20 23:50:25 +08:00
|
|
|
inputs(func(v any) {
|
2021-12-26 20:18:28 +08:00
|
|
|
if errOut != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errOut = out.Put(v)
|
|
|
|
})
|
|
|
|
return errOut
|
|
|
|
}
|
|
|
|
|
|
|
|
func one(fm *Frame, inputs Inputs) error {
|
2022-03-20 23:50:25 +08:00
|
|
|
var val any
|
2021-12-26 20:18:28 +08:00
|
|
|
n := 0
|
2022-03-20 23:50:25 +08:00
|
|
|
inputs(func(v any) {
|
2021-12-26 20:18:28 +08:00
|
|
|
if n == 0 {
|
|
|
|
val = v
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
})
|
|
|
|
if n == 1 {
|
|
|
|
return fm.ValueOutput().Put(val)
|
|
|
|
}
|
|
|
|
return errs.ArityMismatch{What: "values", ValidLow: 1, ValidHigh: 1, Actual: n}
|
|
|
|
}
|
|
|
|
|
|
|
|
func take(fm *Frame, n int, inputs Inputs) error {
|
|
|
|
out := fm.ValueOutput()
|
|
|
|
var errOut error
|
|
|
|
i := 0
|
2022-03-20 23:50:25 +08:00
|
|
|
inputs(func(v any) {
|
2021-12-26 20:18:28 +08:00
|
|
|
if errOut != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if i < n {
|
|
|
|
errOut = out.Put(v)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
})
|
|
|
|
return errOut
|
|
|
|
}
|
|
|
|
|
|
|
|
func drop(fm *Frame, n int, inputs Inputs) error {
|
|
|
|
out := fm.ValueOutput()
|
|
|
|
var errOut error
|
|
|
|
i := 0
|
2022-03-20 23:50:25 +08:00
|
|
|
inputs(func(v any) {
|
2021-12-26 20:18:28 +08:00
|
|
|
if errOut != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if i >= n {
|
|
|
|
errOut = out.Put(v)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
})
|
|
|
|
return errOut
|
|
|
|
}
|
|
|
|
|
2022-08-29 05:57:37 +08:00
|
|
|
func compact(fm *Frame, inputs Inputs) error {
|
|
|
|
out := fm.ValueOutput()
|
|
|
|
first := true
|
|
|
|
var errOut error
|
|
|
|
var prev any
|
|
|
|
|
|
|
|
inputs(func(v any) {
|
|
|
|
if errOut != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if first || !vals.Equal(v, prev) {
|
|
|
|
errOut = out.Put(v)
|
|
|
|
first = false
|
|
|
|
prev = v
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return errOut
|
|
|
|
}
|
|
|
|
|
2021-12-26 20:18:28 +08:00
|
|
|
// The count implementation uses a custom varargs based implementation rather
|
|
|
|
// than the more common `Inputs` API (see pkg/eval/go_fn.go) because this
|
|
|
|
// allows the implementation to be O(1) for the common cases rather than O(n).
|
2022-03-20 23:50:25 +08:00
|
|
|
func count(fm *Frame, args ...any) (int, error) {
|
2021-12-26 20:18:28 +08:00
|
|
|
var n int
|
|
|
|
switch nargs := len(args); nargs {
|
|
|
|
case 0:
|
|
|
|
// Count inputs.
|
2022-03-20 23:50:25 +08:00
|
|
|
fm.IterateInputs(func(any) {
|
2021-12-26 20:18:28 +08:00
|
|
|
n++
|
|
|
|
})
|
|
|
|
case 1:
|
|
|
|
// Get length of argument.
|
|
|
|
v := args[0]
|
|
|
|
if len := vals.Len(v); len >= 0 {
|
|
|
|
n = len
|
|
|
|
} else {
|
2022-03-20 23:50:25 +08:00
|
|
|
err := vals.Iterate(v, func(any) bool {
|
2021-12-26 20:18:28 +08:00
|
|
|
n++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot get length of a %s", vals.Kind(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// The error matches what would be returned if the `Inputs` API was
|
|
|
|
// used. See GoFn.Call().
|
|
|
|
return 0, errs.ArityMismatch{What: "arguments", ValidLow: 0, ValidHigh: 1, Actual: nargs}
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type orderOptions struct {
|
|
|
|
Reverse bool
|
2022-11-21 03:10:19 +08:00
|
|
|
Key Callable
|
2023-05-19 06:18:29 +08:00
|
|
|
Total bool
|
2021-12-26 20:18:28 +08:00
|
|
|
LessThan Callable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opt *orderOptions) SetDefaultOptions() {}
|
|
|
|
|
2023-05-19 06:18:29 +08:00
|
|
|
// ErrBothTotalAndLessThan is returned by order when both the &total and
|
|
|
|
// &less-than options are specified.
|
|
|
|
var ErrBothTotalAndLessThan = errors.New("both &total and &less-than specified")
|
|
|
|
|
2021-12-26 20:18:28 +08:00
|
|
|
func order(fm *Frame, opts orderOptions, inputs Inputs) error {
|
2023-05-19 06:18:29 +08:00
|
|
|
if opts.Total && opts.LessThan != nil {
|
|
|
|
return ErrBothTotalAndLessThan
|
|
|
|
}
|
2022-11-21 05:46:06 +08:00
|
|
|
var values, keys []any
|
|
|
|
inputs(func(v any) { values = append(values, v) })
|
|
|
|
if opts.Key != nil {
|
|
|
|
keys = make([]any, len(values))
|
|
|
|
for i, value := range values {
|
|
|
|
outputs, err := fm.CaptureOutput(func(fm *Frame) error {
|
|
|
|
return opts.Key.Call(fm, []any{value}, NoOpts)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(outputs) != 1 {
|
|
|
|
return errs.ArityMismatch{
|
|
|
|
What: "number of outputs of the &key callback",
|
|
|
|
ValidLow: 1, ValidHigh: 1, Actual: len(outputs)}
|
|
|
|
}
|
|
|
|
keys[i] = outputs[0]
|
|
|
|
}
|
2022-11-21 03:10:19 +08:00
|
|
|
}
|
|
|
|
|
2023-05-19 06:18:29 +08:00
|
|
|
s := &slice{fm, opts.Total, opts.LessThan, values, keys, nil}
|
2022-11-21 05:46:06 +08:00
|
|
|
if opts.Reverse {
|
|
|
|
sort.Stable(sort.Reverse(s))
|
|
|
|
} else {
|
|
|
|
sort.Stable(s)
|
2022-11-21 03:10:19 +08:00
|
|
|
}
|
2022-11-21 05:46:06 +08:00
|
|
|
if s.err != nil {
|
|
|
|
return s.err
|
2022-11-21 03:10:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out := fm.ValueOutput()
|
|
|
|
for _, v := range values {
|
2022-11-21 05:46:06 +08:00
|
|
|
err := out.Put(v)
|
2022-11-21 03:10:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 05:46:06 +08:00
|
|
|
type slice struct {
|
|
|
|
fm *Frame
|
2023-05-19 06:18:29 +08:00
|
|
|
total bool
|
2022-11-21 05:46:06 +08:00
|
|
|
lessThan Callable
|
|
|
|
values []any
|
|
|
|
keys []any // nil if no keys
|
|
|
|
err error
|
2022-11-21 03:10:19 +08:00
|
|
|
}
|
|
|
|
|
2022-11-21 05:46:06 +08:00
|
|
|
func (s *slice) Len() int { return len(s.values) }
|
|
|
|
|
|
|
|
func (s *slice) Less(i, j int) bool {
|
|
|
|
if s.err != nil {
|
|
|
|
return true
|
2022-11-21 03:10:19 +08:00
|
|
|
}
|
2021-12-26 20:18:28 +08:00
|
|
|
|
2022-11-21 05:46:06 +08:00
|
|
|
var a, b any
|
|
|
|
if s.keys != nil {
|
|
|
|
a, b = s.keys[i], s.keys[j]
|
|
|
|
} else {
|
|
|
|
a, b = s.values[i], s.values[j]
|
2021-12-26 20:18:28 +08:00
|
|
|
}
|
|
|
|
|
2022-11-21 05:46:06 +08:00
|
|
|
if s.lessThan == nil {
|
2023-05-19 06:18:29 +08:00
|
|
|
// Use a builtin comparator depending on s.mixed.
|
|
|
|
if s.total {
|
|
|
|
return vals.CmpTotal(a, b) == vals.CmpLess
|
|
|
|
}
|
2023-05-18 17:27:22 +08:00
|
|
|
o := vals.Cmp(a, b)
|
|
|
|
if o == vals.CmpUncomparable {
|
2022-11-21 05:46:06 +08:00
|
|
|
s.err = ErrUncomparable
|
2022-11-21 03:10:19 +08:00
|
|
|
return true
|
|
|
|
}
|
2023-05-18 17:27:22 +08:00
|
|
|
return o == vals.CmpLess
|
2021-12-26 20:18:28 +08:00
|
|
|
}
|
2022-11-21 05:46:06 +08:00
|
|
|
|
|
|
|
// Use the &less-than callback.
|
|
|
|
outputs, err := s.fm.CaptureOutput(func(fm *Frame) error {
|
|
|
|
return s.lessThan.Call(fm, []any{a, b}, NoOpts)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(outputs) != 1 {
|
|
|
|
s.err = errs.ArityMismatch{
|
|
|
|
What: "number of outputs of the &less-than callback",
|
|
|
|
ValidLow: 1, ValidHigh: 1, Actual: len(outputs)}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if b, ok := outputs[0].(bool); ok {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
s.err = errs.BadValue{
|
|
|
|
What: "output of the &less-than callback",
|
|
|
|
Valid: "boolean", Actual: vals.Kind(outputs[0])}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slice) Swap(i, j int) {
|
|
|
|
s.values[i], s.values[j] = s.values[j], s.values[i]
|
|
|
|
if s.keys != nil {
|
|
|
|
s.keys[i], s.keys[j] = s.keys[j], s.keys[i]
|
|
|
|
}
|
2021-12-26 20:18:28 +08:00
|
|
|
}
|