-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
builtin_fn_container.go
223 lines (195 loc) · 4.14 KB
/
builtin_fn_container.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package eval
import (
"errors"
"fmt"
"io"
"github.com/elves/elvish/eval/vals"
"github.com/elves/elvish/eval/vars"
"github.com/xiaq/persistent/hashmap"
)
// Sequence, list and maps.
func init() {
addBuiltinFns(map[string]interface{}{
"ns": nsFn,
"range": rangeFn,
"repeat": repeat,
"explode": explode,
"assoc": assoc,
"dissoc": dissoc,
"all": all,
"has-key": hasKey,
"has-value": hasValue,
"take": take,
"drop": drop,
"count": count,
"keys": keys,
})
}
var errKeyMustBeString = errors.New("key must be string")
func nsFn(m hashmap.Map) (Ns, error) {
ns := make(Ns)
for it := m.Iterator(); it.HasElem(); it.Next() {
k, v := it.Elem()
kstring, ok := k.(string)
if !ok {
return nil, errKeyMustBeString
}
ns[kstring] = vars.NewAnyWithInit(v)
}
return ns, nil
}
func rangeFn(fm *Frame, rawOpts RawOptions, args ...float64) error {
opts := struct{ Step float64 }{1}
rawOpts.Scan(&opts)
var lower, upper float64
switch len(args) {
case 1:
upper = args[0]
case 2:
lower, upper = args[0], args[1]
default:
return ErrArgs
}
out := fm.ports[1].Chan
for f := lower; f < upper; f += opts.Step {
out <- vals.FromGo(f)
}
return nil
}
func repeat(fm *Frame, n int, v interface{}) {
out := fm.OutputChan()
for i := 0; i < n; i++ {
out <- v
}
}
// explode puts each element of the argument.
func explode(fm *Frame, v interface{}) error {
out := fm.ports[1].Chan
return vals.Iterate(v, func(e interface{}) bool {
out <- e
return true
})
}
func assoc(a, k, v interface{}) (interface{}, error) {
return vals.Assoc(a, k, v)
}
var errCannotDissoc = errors.New("cannot dissoc")
func dissoc(a, k interface{}) (interface{}, error) {
a2 := vals.Dissoc(a, k)
if a2 == nil {
return nil, errCannotDissoc
}
return a2, nil
}
func all(fm *Frame) error {
valuesDone := make(chan struct{})
go func() {
for input := range fm.ports[0].Chan {
fm.ports[1].Chan <- input
}
close(valuesDone)
}()
_, err := io.Copy(fm.ports[1].File, fm.ports[0].File)
<-valuesDone
if err != nil {
return fmt.Errorf("cannot copy byte input: %s", err)
}
return nil
}
func take(fm *Frame, n int, inputs Inputs) {
out := fm.ports[1].Chan
i := 0
inputs(func(v interface{}) {
if i < n {
out <- v
}
i++
})
}
func drop(fm *Frame, n int, inputs Inputs) {
out := fm.ports[1].Chan
i := 0
inputs(func(v interface{}) {
if i >= n {
out <- v
}
i++
})
}
func hasValue(container, value interface{}) (bool, error) {
switch container := container.(type) {
case hashmap.Map:
for it := container.Iterator(); it.HasElem(); it.Next() {
_, v := it.Elem()
if vals.Equal(v, value) {
return true, nil
}
}
return false, nil
default:
var found bool
err := vals.Iterate(container, func(v interface{}) bool {
found = (v == value)
return !found
})
return found, err
}
}
func hasKey(container, key interface{}) (bool, error) {
switch container := container.(type) {
case hashmap.Map:
return hashmap.HasKey(container, key), nil
default:
if len := vals.Len(container); len >= 0 {
// XXX(xiaq): Not all types that implement Lener have numerical indices
_, err := vals.ConvertListIndex(key, len)
return err == nil, nil
} else {
var found bool
err := vals.IterateKeys(container, func(k interface{}) bool {
if key == k {
found = true
}
return !found
})
if err == nil {
return found, nil
}
}
return false, fmt.Errorf("couldn't get key or index of type '%s'", vals.Kind(container))
}
}
func count(fm *Frame, args ...interface{}) (int, error) {
var n int
switch len(args) {
case 0:
// Count inputs.
fm.IterateInputs(func(interface{}) {
n++
})
case 1:
// Get length of argument.
v := args[0]
if len := vals.Len(v); len >= 0 {
n = len
} else {
err := vals.Iterate(v, func(interface{}) bool {
n++
return true
})
if err != nil {
return 0, fmt.Errorf("cannot get length of a %s", vals.Kind(v))
}
}
default:
return 0, errors.New("want 0 or 1 argument")
}
return n, nil
}
func keys(fm *Frame, v interface{}) error {
out := fm.ports[1].Chan
return vals.IterateKeys(v, func(k interface{}) bool {
out <- k
return true
})
}