-
Notifications
You must be signed in to change notification settings - Fork 15
/
expr_darrow.go
49 lines (43 loc) · 1.24 KB
/
expr_darrow.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
package rel
import (
"fmt"
"github.com/arr-ai/wbnf/parser"
"github.com/go-errors/errors"
)
// DArrowExpr returns the set applied elementwise to a function.
type DArrowExpr struct {
ExprScanner
lhs Expr
fn *Function
}
// NewDArrowExpr returns a new DArrowExpr.
func NewDArrowExpr(scanner parser.Scanner, lhs Expr, fn Expr) Expr {
return &DArrowExpr{ExprScanner{scanner}, lhs, ExprAsFunction(fn)}
}
// String returns a string representation of the expression.
func (e *DArrowExpr) String() string {
return fmt.Sprintf("(%s => %s)", e.lhs, e.fn)
}
// Eval returns the lhs transformed elementwise by fn.
func (e *DArrowExpr) Eval(local Scope) (_ Value, err error) {
value, err := e.lhs.Eval(local)
if err != nil {
return nil, WrapContext(err, e, local)
}
if set, ok := value.(Set); ok {
values := []Value{}
for i := set.Enumerator(); i.MoveNext(); {
scope, err := e.fn.arg.Bind(local, i.Current())
if err != nil {
return nil, WrapContext(err, e, local)
}
v, err := e.fn.body.Eval(local.Update(scope))
if err != nil {
return nil, WrapContext(err, e, local)
}
values = append(values, v)
}
return NewSet(values...), nil
}
return nil, WrapContext(errors.Errorf("=> not applicable to %T: %[1]v", value), e, local)
}