forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_map.go
33 lines (29 loc) · 817 Bytes
/
filter_map.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
package optimizer
import (
. "github.com/expr-lang/expr/ast"
)
type filterMap struct{}
func (*filterMap) Visit(node *Node) {
if mapBuiltin, ok := (*node).(*BuiltinNode); ok &&
mapBuiltin.Name == "map" &&
len(mapBuiltin.Arguments) == 2 &&
Find(mapBuiltin.Arguments[1], isIndexPointer) == nil {
if predicate, ok := mapBuiltin.Arguments[1].(*PredicateNode); ok {
if filter, ok := mapBuiltin.Arguments[0].(*BuiltinNode); ok &&
filter.Name == "filter" &&
filter.Map == nil /* not already optimized */ {
patchCopyType(node, &BuiltinNode{
Name: "filter",
Arguments: filter.Arguments,
Map: predicate.Node,
})
}
}
}
}
func isIndexPointer(node Node) bool {
if pointer, ok := node.(*PointerNode); ok && pointer.Name == "index" {
return true
}
return false
}