-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlist.go
More file actions
293 lines (275 loc) · 10.8 KB
/
list.go
File metadata and controls
293 lines (275 loc) · 10.8 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
package lib
import "github.com/islisp-dev/iris/core"
// Listp returns t if obj is a list (instance of class list); otherwise, returns
// nil. obj may be any ISLISP object.
func Listp(e core.Environment, obj core.Instance) (core.Instance, core.Instance) {
if core.InstanceOf(core.ListClass, obj) {
return T, nil
}
return Nil, nil
}
// CreateList returns a list of length i. If initial-element is given, the
// elements of the new list are initialized with this object; otherwise, the
// initialization is implementation defined. An error shall be signaled if the
// requested list cannot be allocated (error-id. cannot-create-list). An error
// shall be signaled if i is not a non-negative integer (error-id.
// domain-error).initial-element may be any ISLISP object.
func CreateList(e core.Environment, i core.Instance, initialElement ...core.Instance) (core.Instance, core.Instance) {
if ok, _ := Integerp(e, i); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, i, core.IntegerClass), Nil)
}
if len(initialElement) > 1 {
return SignalCondition(e, core.NewArityError(e), Nil)
}
elm := Nil
if len(initialElement) == 1 {
elm = initialElement[0]
}
cons := Nil
for j := 0; j < int(i.(core.Integer)); j++ {
cons = core.NewCons(elm, cons)
}
return cons, nil
}
// List returns a new list whose length is the number of arguments and whose
// elements are the arguments in the same order as in the list-form. An error
// shall be signaled if the requested list cannot be allocated (error-id.
// cannot-create-list). Each obj may be any ISLISP object.
func List(e core.Environment, objs ...core.Instance) (core.Instance, core.Instance) {
cons := Nil
for i := len(objs) - 1; i >= 0; i-- {
cons = core.NewCons(objs[i], cons)
}
return cons, nil
}
// Reverse returns a list whose elements are those of the given list, but in
// reverse order. An error shall be signaled if list is not a list (error-id.
// domain-error). For reverse, no side-effect to the given list occurs. The
// resulting list is permitted but not required to share structure with the
// input list.
func Reverse(e core.Environment, list core.Instance) (core.Instance, core.Instance) {
if ok, _ := Listp(e, list); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, list, core.ListClass), Nil)
}
cons := Nil
for _, car := range list.(core.List).Slice() {
cons = core.NewCons(car, cons)
}
return cons, nil
}
// Nreverse returns a list whose elements are those of the given list, but in
// reverse order. An error shall be signaled if list is not a list (error-id.
// domain-error). For nreverse, the conses which make up the top level of the
// given list are permitted, but not required, to be side-effected in order to
// produce this new list. nreverse should never be called on a literal object.
func Nreverse(e core.Environment, list core.Instance) (core.Instance, core.Instance) {
// TODO: tests literal object
if ok, _ := Listp(e, list); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, list, core.ListClass), Nil)
}
cons := Nil
for _, car := range list.(core.List).Slice() {
cons = core.NewCons(car, cons)
}
return cons, nil
}
// Append returns the result of appending all of the lists, or () if given no
// lists. An error shall be signaled if any list is not a list (error-id.
// domain-error). This function does not modify its arguments. It is
// implementation defined whether and when the result shares structure with its
// list arguments. An error shall be signaled if the list cannot be allocated
// (error-id. cannot-create-list).
func Append(e core.Environment, lists ...core.Instance) (core.Instance, core.Instance) {
// Ref: https://github.com/sbcl/sbcl/blob/fe4faef65315c6ad52b3b89b62b6c6497cb78d09/src/code/list.lisp#L364
result, err := List(e, Nil)
if err != nil {
return nil, err
}
cdr := result
for _, list := range lists {
if ok, _ := Listp(e, list); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, list, core.ListClass), Nil)
}
}
for _, list := range lists {
for _, elt := range list.(core.List).Slice() {
it, err := List(e, elt)
if err != nil {
return nil, err
}
cdr.(*core.Cons).Cdr = it
cdr = cdr.(*core.Cons).Cdr
}
}
return result.(*core.Cons).Cdr, nil
}
// Member returnes the first sublist of list whose car is obj if list contains
// at least one occurrence of obj (as determined by eql). Otherwise, nil is
// returned. An error shall be signaled if list is not a list (error-id.
// domain-error).
func Member(e core.Environment, obj, list core.Instance) (core.Instance, core.Instance) {
if ok, _ := Listp(e, list); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, list, core.ListClass), Nil)
}
if !core.InstanceOf(core.ConsClass, list) || core.DeepEqual(list.(*core.Cons).Car, obj) {
return list, nil
}
if !core.InstanceOf(core.ConsClass, list.(*core.Cons).Cdr) {
return list.(*core.Cons).Cdr, nil
}
return Member(e, obj, list.(*core.Cons).Cdr)
}
// Mapcar operates on successive elements of the lists. function is applied to
// the first element of each list, then to the second element of each list, and
// so on. The iteration terminates when the shortest list runs out, and excess
// elements in other lists are ignored. The value returned by mapcar is a list
// of the results of successive calls to function.
func Mapcar(e core.Environment, function, list1 core.Instance, lists ...core.Instance) (core.Instance, core.Instance) {
lists = append([]core.Instance{list1}, lists...)
if ok, _ := Functionp(e, function); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, function, core.FunctionClass), Nil)
}
for _, list := range lists {
if ok, _ := Listp(e, list); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, list, core.ListClass), Nil)
}
}
arguments := []core.Instance{}
rests := []core.Instance{}
for _, list := range lists {
if core.DeepEqual(list, Nil) {
return Nil, nil
}
arguments = append(arguments, list.(*core.Cons).Car)
rests = append(rests, list.(*core.Cons).Cdr)
}
car, err := function.(core.Applicable).Apply(e.NewDynamic(), arguments...)
if err != nil {
return nil, err
}
var cdr core.Instance
for _, list := range lists {
if ok, _ := Listp(e, list); core.DeepEqual(ok, Nil) {
cdr = Nil
}
}
if core.DeepEqual(cdr, nil) {
cdr, err = Mapcar(e, function, rests[0], rests[1:]...)
if err != nil {
return nil, err
}
}
return Cons(e, car, cdr)
}
// Mapc is like mapcar except that the results of applying function are not
// accumulated; list1 is returned.
func Mapc(e core.Environment, function, list1 core.Instance, lists ...core.Instance) (core.Instance, core.Instance) {
_, err := Mapcar(e, function, list1, lists...)
if err != nil {
return nil, err
}
return list1, nil
}
// Mapcan is like mapcar respectively, except that the results of applying
// function are combined into a list by the use of an operation that performs a
// destructive form of append rather than list.
func Mapcan(e core.Environment, function, list1 core.Instance, lists ...core.Instance) (core.Instance, core.Instance) {
list, err := Mapcar(e, function, list1, lists...)
if err != nil {
return nil, err
}
append, _ := e.Function.Get(core.NewSymbol("APPEND"))
return Apply(e, append, list)
}
// Maplist is like mapcar except that function is applied to successive sublists
// of the lists. function is first applied to the lists themselves, and then to
// the cdr of each list, and then to the cdr of the cdr of each list, and so on.
func Maplist(e core.Environment, function, list1 core.Instance, lists ...core.Instance) (core.Instance, core.Instance) {
lists = append([]core.Instance{list1}, lists...)
if ok, _ := Functionp(e, function); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, function, core.FunctionClass), Nil)
}
for _, list := range lists {
if ok, _ := Listp(e, list); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, list, core.ListClass), Nil)
}
}
arguments := []core.Instance{}
rests := []core.Instance{}
for _, list := range lists {
if core.DeepEqual(list, Nil) {
return Nil, nil
}
arguments = append(arguments, list)
rests = append(rests, list.(*core.Cons).Cdr)
}
car, err := function.(core.Applicable).Apply(e.NewDynamic(), arguments...)
if err != nil {
return nil, err
}
var cdr core.Instance
for _, list := range lists {
if ok, _ := Listp(e, list); core.DeepEqual(ok, Nil) {
cdr = Nil
}
}
if core.DeepEqual(cdr, nil) {
cdr, err = Maplist(e, function, rests[0], rests[1:]...)
if err != nil {
return nil, err
}
}
return Cons(e, car, cdr)
}
// Mapl is like maplist except that the results of applying function are not
// accumulated; list1 is returned.
func Mapl(e core.Environment, function, list1 core.Instance, lists ...core.Instance) (core.Instance, core.Instance) {
_, err := Maplist(e, function, list1, lists...)
if err != nil {
return nil, err
}
return list1, nil
}
// Mapcon is like maplist respectively, except that the results of applying
// function are combined into a list by the use of an operation that performs a
// destructive form of append rather than list.
func Mapcon(e core.Environment, function, list1 core.Instance, lists ...core.Instance) (core.Instance, core.Instance) {
list, err := Maplist(e, function, list1, lists...)
if err != nil {
return nil, err
}
append, _ := e.Function.Get(core.NewSymbol("APPEND"))
return Apply(e, append, list)
}
// Assoc returns the first cons if assocation-list contains at least one cons
// whose car is obj (as determined by eql). Otherwise, nil is returned. An error
// shall be signaled if association-list is not a list of conses (error-id.
// domain-error).
func Assoc(e core.Environment, obj, associationList core.Instance) (core.Instance, core.Instance) {
if ok, _ := Listp(e, associationList); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, associationList, core.ListClass), Nil)
}
if !core.InstanceOf(core.ConsClass, associationList) {
return Nil, nil
}
car := associationList.(*core.Cons).Car
cdr := associationList.(*core.Cons).Cdr
if ok, _ := Consp(e, car); core.DeepEqual(ok, Nil) {
return SignalCondition(e, core.NewDomainError(e, car, core.ConsClass), Nil)
}
if core.DeepEqual(car.(*core.Cons).Car, obj) { // eql
return car, nil
}
return Assoc(e, obj, cdr)
}
// Null returns t if obj is nil; otherwise, returns nil obj may be any ISLISP
// object.
func Null(e core.Environment, obj core.Instance) (core.Instance, core.Instance) {
if core.DeepEqual(obj, Nil) {
return T, nil
}
return Nil, nil
}