/
functional.go
204 lines (176 loc) · 4.94 KB
/
functional.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
package core
import (
"fmt"
"github.com/inoxlang/inox/internal/core/symbolic"
)
func init() {
RegisterSymbolicGoFunctions([]any{
Filter, func(ctx *symbolic.Context, iterable symbolic.Iterable, cond symbolic.Value) *symbolic.List {
return symbolic.NewListOf(symbolic.ANY_SERIALIZABLE)
},
GetAtMost, func(ctx *symbolic.Context, maxCount *symbolic.Int, iterable symbolic.SerializableIterable) *symbolic.List {
element, ok := symbolic.AsSerializable(iterable.IteratorElementValue()).(symbolic.Serializable)
if !ok {
element = symbolic.ANY_SERIALIZABLE
ctx.AddSymbolicGoFunctionError("elements of the iterable are not serializable")
}
return symbolic.NewListOf(element)
},
Some, func(ctx *symbolic.Context, iterable symbolic.Iterable, cond symbolic.Value) *symbolic.Bool {
return symbolic.ANY_BOOL
},
All, func(ctx *symbolic.Context, iterable symbolic.Iterable, cond symbolic.Value) *symbolic.Bool {
return symbolic.ANY_BOOL
},
None, func(ctx *symbolic.Context, iterable symbolic.Iterable, cond symbolic.Value) *symbolic.Bool {
return symbolic.ANY_BOOL
},
})
}
// Filter is the value of the 'filter' global.
func Filter(ctx *Context, iterable Iterable, condition Value) *List {
result := ValueList{}
switch fil := condition.(type) {
case AstNode:
state := ctx.GetClosestState()
treeWalkState := NewTreeWalkStateWithGlobal(state)
treeWalkState.PushScope()
defer treeWalkState.PopScope()
it := iterable.Iterator(ctx, IteratorConfiguration{})
for it.Next(ctx) {
e := it.Value(ctx)
treeWalkState.CurrentLocalScope()[""] = e
res, err := TreeWalkEval(fil.Node, treeWalkState)
if err != nil {
panic(err)
}
if res.(Bool) {
result.elements = append(result.elements, e.(Serializable))
}
}
case Pattern:
it := iterable.Iterator(ctx, IteratorConfiguration{})
for it.Next(ctx) {
e := it.Value(ctx)
if fil.Test(ctx, e) {
result.elements = append(result.elements, e.(Serializable))
}
}
default:
panic(fmt.Errorf("invalid filter : type is %T", fil))
}
return WrapUnderlyingList(&result)
}
// GetAtMost is the value of the 'get_at_most' global.
func GetAtMost(ctx *Context, maxCount Int, iterable SerializableIterable) *List {
var elements []Serializable
count := 0
if indexable, ok := iterable.(Indexable); ok {
end := min(int(maxCount), indexable.Len())
for i := 0; i < end; i++ {
elements = append(elements, indexable.At(ctx, i).(Serializable))
}
} else {
it := iterable.Iterator(ctx, IteratorConfiguration{
KeysNeverRead: true,
})
for count < int(maxCount) && it.Next(ctx) {
elements = append(elements, it.Value(ctx).(Serializable))
count++
}
}
return NewWrappedValueListFrom(elements)
}
// Some is the value of the 'some' global.
func Some(ctx *Context, iterable Iterable, condition Value) Bool {
state := ctx.GetClosestState()
treeWalkState := NewTreeWalkStateWithGlobal(state)
treeWalkState.PushScope()
defer treeWalkState.PopScope()
switch cond := condition.(type) {
case AstNode:
it := iterable.Iterator(ctx, IteratorConfiguration{})
for it.Next(ctx) {
e := it.Value(ctx)
treeWalkState.CurrentLocalScope()[""] = e
res, err := TreeWalkEval(cond.Node, treeWalkState)
if err != nil {
panic(err)
}
if res.(Bool) {
return true
}
}
case Pattern:
it := iterable.Iterator(ctx, IteratorConfiguration{})
for it.Next(ctx) {
e := it.Value(ctx)
if cond.Test(ctx, e) {
return true
}
}
}
return true
}
// All is the value of the 'all' global.
func All(ctx *Context, iterable Iterable, condition Value) Bool {
state := ctx.GetClosestState()
treeWalkState := NewTreeWalkStateWithGlobal(state)
treeWalkState.PushScope()
defer treeWalkState.PopScope()
switch cond := condition.(type) {
case AstNode:
it := iterable.Iterator(ctx, IteratorConfiguration{})
for it.Next(ctx) {
e := it.Value(ctx)
treeWalkState.CurrentLocalScope()[""] = e
res, err := TreeWalkEval(cond.Node, treeWalkState)
if err != nil {
panic(err)
}
if !res.(Bool) {
return false
}
}
case Pattern:
it := iterable.Iterator(ctx, IteratorConfiguration{})
for it.Next(ctx) {
e := it.Value(ctx)
if !cond.Test(ctx, e) {
return false
}
}
}
return true
}
// None is the value of the 'none' global.
func None(ctx *Context, iterable Iterable, condition Value) Bool {
state := ctx.GetClosestState()
treeWalkState := NewTreeWalkStateWithGlobal(state)
treeWalkState.PushScope()
defer treeWalkState.PopScope()
switch cond := condition.(type) {
case AstNode:
it := iterable.Iterator(ctx, IteratorConfiguration{})
for it.Next(ctx) {
e := it.Value(ctx)
treeWalkState.CurrentLocalScope()[""] = e
res, err := TreeWalkEval(cond.Node, treeWalkState)
if err != nil {
panic(err)
}
if res.(Bool) {
return false
}
}
case Pattern:
it := iterable.Iterator(ctx, IteratorConfiguration{})
for it.Next(ctx) {
e := it.Value(ctx)
if cond.Test(ctx, e) {
return false
}
}
}
return true
}