-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_fn_container.go
316 lines (293 loc) · 6.09 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package eval
import (
"errors"
"fmt"
"github.com/markusbkk/elvish/pkg/eval/errs"
"github.com/markusbkk/elvish/pkg/eval/vals"
"github.com/markusbkk/elvish/pkg/eval/vars"
)
// Lists and maps.
func init() {
addBuiltinFns(map[string]interface{}{
"ns": nsFn,
"make-map": makeMap,
"assoc": assoc,
"dissoc": dissoc,
"has-key": hasKey,
"has-value": hasValue,
"keys": keys,
})
}
//elvdoc:fn ns
//
// ```elvish
// ns $map
// ```
//
// Constructs a namespace from `$map`, using the keys as variable names and the
// values as their values. Examples:
//
// ```elvish-transcript
// ~> var n = (ns [&name=value])
// ~> put $n[name]
// ▶ value
// ~> var n: = (ns [&name=value])
// ~> put $n:name
// ▶ value
// ```
func nsFn(m vals.Map) (*Ns, error) {
nb := BuildNs()
for it := m.Iterator(); it.HasElem(); it.Next() {
k, v := it.Elem()
kstring, ok := k.(string)
if !ok {
return nil, errs.BadValue{
What: `key of argument of "ns"`,
Valid: "string", Actual: vals.Kind(k)}
}
nb.AddVar(kstring, vars.FromInit(v))
}
return nb.Ns(), nil
}
//elvdoc:fn make-map
//
// ```elvish
// make-map $input?
// ```
//
// Outputs a map from the [value inputs](#value-inputs), each of which must be
// an iterable value with with two elements. The first element of each value
// is used as the key, and the second element is used as the value.
//
// If the same key appears multiple times, the last value is used.
//
// Examples:
//
// ```elvish-transcript
// ~> make-map [[k v]]
// ▶ [&k=v]
// ~> make-map [[k v1] [k v2]]
// ▶ [&k=v2]
// ~> put [k1 v1] [k2 v2] | make-map
// ▶ [&k1=v1 &k2=v2]
// ~> put aA bB | make-map
// ▶ [&a=A &b=B]
// ```
func makeMap(input Inputs) (vals.Map, error) {
m := vals.EmptyMap
var errMakeMap error
input(func(v interface{}) {
if errMakeMap != nil {
return
}
if !vals.CanIterate(v) {
errMakeMap = errs.BadValue{
What: "input to make-map", Valid: "iterable", Actual: vals.Kind(v)}
return
}
if l := vals.Len(v); l != 2 {
errMakeMap = errs.BadValue{
What: "input to make-map", Valid: "iterable with 2 elements",
Actual: fmt.Sprintf("%v with %v elements", vals.Kind(v), l)}
return
}
elems, err := vals.Collect(v)
if err != nil {
errMakeMap = err
return
}
if len(elems) != 2 {
errMakeMap = fmt.Errorf("internal bug: collected %v values", len(elems))
return
}
m = m.Assoc(elems[0], elems[1])
})
return m, errMakeMap
}
//elvdoc:fn assoc
//
// ```elvish
// assoc $container $k $v
// ```
//
// Output a slightly modified version of `$container`, such that its value at `$k`
// is `$v`. Applies to both lists and to maps.
//
// When `$container` is a list, `$k` may be a negative index. However, slice is not
// yet supported.
//
// ```elvish-transcript
// ~> assoc [foo bar quux] 0 lorem
// ▶ [lorem bar quux]
// ~> assoc [foo bar quux] -1 ipsum
// ▶ [foo bar ipsum]
// ~> assoc [&k=v] k v2
// ▶ [&k=v2]
// ~> assoc [&k=v] k2 v2
// ▶ [&k2=v2 &k=v]
// ```
//
// Etymology: [Clojure](https://clojuredocs.org/clojure.core/assoc).
//
// @cf dissoc
func assoc(a, k, v interface{}) (interface{}, error) {
return vals.Assoc(a, k, v)
}
var errCannotDissoc = errors.New("cannot dissoc")
//elvdoc:fn dissoc
//
// ```elvish
// dissoc $map $k
// ```
//
// Output a slightly modified version of `$map`, with the key `$k` removed. If
// `$map` does not contain `$k` as a key, the same map is returned.
//
// ```elvish-transcript
// ~> dissoc [&foo=bar &lorem=ipsum] foo
// ▶ [&lorem=ipsum]
// ~> dissoc [&foo=bar &lorem=ipsum] k
// ▶ [&lorem=ipsum &foo=bar]
// ```
//
// @cf assoc
func dissoc(a, k interface{}) (interface{}, error) {
a2 := vals.Dissoc(a, k)
if a2 == nil {
return nil, errCannotDissoc
}
return a2, nil
}
//elvdoc:fn has-value
//
// ```elvish
// has-value $container $value
// ```
//
// Determine whether `$value` is a value in `$container`.
//
// Examples, maps:
//
// ```elvish-transcript
// ~> has-value [&k1=v1 &k2=v2] v1
// ▶ $true
// ~> has-value [&k1=v1 &k2=v2] k1
// ▶ $false
// ```
//
// Examples, lists:
//
// ```elvish-transcript
// ~> has-value [v1 v2] v1
// ▶ $true
// ~> has-value [v1 v2] k1
// ▶ $false
// ```
//
// Examples, strings:
//
// ```elvish-transcript
// ~> has-value ab b
// ▶ $true
// ~> has-value ab c
// ▶ $false
// ```
func hasValue(container, value interface{}) (bool, error) {
switch container := container.(type) {
case vals.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
}
}
//elvdoc:fn has-key
//
// ```elvish
// has-key $container $key
// ```
//
// Determine whether `$key` is a key in `$container`. A key could be a map key or
// an index on a list or string. This includes a range of indexes.
//
// Examples, maps:
//
// ```elvish-transcript
// ~> has-key [&k1=v1 &k2=v2] k1
// ▶ $true
// ~> has-key [&k1=v1 &k2=v2] v1
// ▶ $false
// ```
//
// Examples, lists:
//
// ```elvish-transcript
// ~> has-key [v1 v2] 0
// ▶ $true
// ~> has-key [v1 v2] 1
// ▶ $true
// ~> has-key [v1 v2] 2
// ▶ $false
// ~> has-key [v1 v2] 0:2
// ▶ $true
// ~> has-key [v1 v2] 0:3
// ▶ $false
// ```
//
// Examples, strings:
//
// ```elvish-transcript
// ~> has-key ab 0
// ▶ $true
// ~> has-key ab 1
// ▶ $true
// ~> has-key ab 2
// ▶ $false
// ~> has-key ab 0:2
// ▶ $true
// ~> has-key ab 0:3
// ▶ $false
// ```
func hasKey(container, key interface{}) bool {
return vals.HasKey(container, key)
}
//elvdoc:fn keys
//
// ```elvish
// keys $map
// ```
//
// Put all keys of `$map` on the structured stdout.
//
// Example:
//
// ```elvish-transcript
// ~> keys [&a=foo &b=bar &c=baz]
// ▶ a
// ▶ c
// ▶ b
// ```
//
// Note that there is no guaranteed order for the keys of a map.
func keys(fm *Frame, v interface{}) error {
out := fm.ValueOutput()
var errPut error
errIterate := vals.IterateKeys(v, func(k interface{}) bool {
errPut = out.Put(k)
return errPut == nil
})
if errIterate != nil {
return errIterate
}
return errPut
}