forked from candid82/joker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq.go
379 lines (316 loc) · 6.27 KB
/
seq.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package core
import (
"bytes"
"fmt"
)
type (
Seq interface {
Seqable
Object
First() Object
Rest() Seq
IsEmpty() bool
Cons(obj Object) Seq
}
Seqable interface {
Seq() Seq
}
SeqIterator struct {
seq Seq
}
ConsSeq struct {
InfoHolder
MetaHolder
first Object
rest Seq
}
ArraySeq struct {
InfoHolder
MetaHolder
arr []Object
index int
}
LazySeq struct {
InfoHolder
MetaHolder
fn Callable
seq Seq
}
MappingSeq struct {
InfoHolder
MetaHolder
seq Seq
fn func(obj Object) Object
}
)
func SeqsEqual(seq1, seq2 Seq) bool {
iter2 := iter(seq2)
for iter1 := iter(seq1); iter1.HasNext(); {
if !iter2.HasNext() || !iter2.Next().Equals(iter1.Next()) {
return false
}
}
return !iter2.HasNext()
}
func IsSeqEqual(seq Seq, other interface{}) bool {
if seq == other {
return true
}
switch other := other.(type) {
case Sequential:
switch other := other.(type) {
case Seqable:
return SeqsEqual(seq, other.Seq())
}
}
return false
}
func (seq *MappingSeq) Seq() Seq {
return seq
}
func (seq *MappingSeq) Equals(other interface{}) bool {
return IsSeqEqual(seq, other)
}
func (seq *MappingSeq) ToString(escape bool) string {
return SeqToString(seq, escape)
}
func (seq *MappingSeq) WithMeta(meta Map) Object {
res := *seq
res.meta = SafeMerge(res.meta, meta)
return &res
}
func (seq *MappingSeq) GetType() *Type {
return TYPES["MappingSeq"]
}
func (seq *MappingSeq) Hash() uint32 {
return hashOrdered(seq)
}
func (seq *MappingSeq) First() Object {
return seq.fn(seq.seq.First())
}
func (seq *MappingSeq) Rest() Seq {
return &MappingSeq{
seq: seq.seq.Rest(),
fn: seq.fn,
}
}
func (seq *MappingSeq) IsEmpty() bool {
return seq.seq.IsEmpty()
}
func (seq *MappingSeq) Cons(obj Object) Seq {
return &ConsSeq{first: obj, rest: seq}
}
func (seq *MappingSeq) sequential() {}
func (seq *LazySeq) Seq() Seq {
return seq
}
func (seq *LazySeq) realize() {
if seq.seq == nil {
seq.seq = AssertSeqable(seq.fn.Call([]Object{}), "").Seq()
}
}
func (seq *LazySeq) IsRealized() bool {
return seq.seq != nil
}
func (seq *LazySeq) Equals(other interface{}) bool {
return IsSeqEqual(seq, other)
}
func (seq *LazySeq) ToString(escape bool) string {
return SeqToString(seq, escape)
}
func (seq *LazySeq) WithMeta(meta Map) Object {
res := *seq
res.meta = SafeMerge(res.meta, meta)
return &res
}
func (seq *LazySeq) GetType() *Type {
return TYPES["LazySeq"]
}
func (seq *LazySeq) Hash() uint32 {
return hashOrdered(seq)
}
func (seq *LazySeq) First() Object {
seq.realize()
return seq.seq.First()
}
func (seq *LazySeq) Rest() Seq {
seq.realize()
return seq.seq.Rest()
}
func (seq *LazySeq) IsEmpty() bool {
seq.realize()
return seq.seq.IsEmpty()
}
func (seq *LazySeq) Cons(obj Object) Seq {
return &ConsSeq{first: obj, rest: seq}
}
func (seq *LazySeq) sequential() {}
func (seq *ArraySeq) Seq() Seq {
return seq
}
func (seq *ArraySeq) Equals(other interface{}) bool {
return IsSeqEqual(seq, other)
}
func (seq *ArraySeq) ToString(escape bool) string {
return SeqToString(seq, escape)
}
func (seq *ArraySeq) WithMeta(meta Map) Object {
res := *seq
res.meta = SafeMerge(res.meta, meta)
return &res
}
func (seq *ArraySeq) GetType() *Type {
return TYPES["ArraySeq"]
}
func (seq *ArraySeq) Hash() uint32 {
return hashOrdered(seq)
}
func (seq *ArraySeq) First() Object {
if seq.IsEmpty() {
return NIL
}
return seq.arr[seq.index]
}
func (seq *ArraySeq) Rest() Seq {
if seq.index+1 < len(seq.arr) {
return &ArraySeq{index: seq.index + 1, arr: seq.arr}
}
return EmptyList
}
func (seq *ArraySeq) IsEmpty() bool {
return seq.index >= len(seq.arr)
}
func (seq *ArraySeq) Cons(obj Object) Seq {
return &ConsSeq{first: obj, rest: seq}
}
func (seq *ArraySeq) sequential() {}
func SeqToString(seq Seq, escape bool) string {
var b bytes.Buffer
b.WriteRune('(')
for iter := iter(seq); iter.HasNext(); {
b.WriteString(iter.Next().ToString(escape))
if iter.HasNext() {
b.WriteRune(' ')
}
}
b.WriteRune(')')
return b.String()
}
func (seq *ConsSeq) WithMeta(meta Map) Object {
res := *seq
res.meta = SafeMerge(res.meta, meta)
return &res
}
func (seq *ConsSeq) Seq() Seq {
return seq
}
func (seq *ConsSeq) Equals(other interface{}) bool {
return IsSeqEqual(seq, other)
}
func (seq *ConsSeq) ToString(escape bool) string {
return SeqToString(seq, escape)
}
func (seq *ConsSeq) GetType() *Type {
return TYPES["ConsSeq"]
}
func (seq *ConsSeq) Hash() uint32 {
return hashOrdered(seq)
}
func (seq *ConsSeq) First() Object {
return seq.first
}
func (seq *ConsSeq) Rest() Seq {
return seq.rest
}
func (seq *ConsSeq) IsEmpty() bool {
return false
}
func (seq *ConsSeq) Cons(obj Object) Seq {
return &ConsSeq{first: obj, rest: seq}
}
func (seq *ConsSeq) sequential() {}
func iter(seq Seq) *SeqIterator {
return &SeqIterator{seq: seq}
}
func (iter *SeqIterator) Next() Object {
res := iter.seq.First()
iter.seq = iter.seq.Rest()
return res
}
func (iter *SeqIterator) HasNext() bool {
return !iter.seq.IsEmpty()
}
func Second(seq Seq) Object {
return seq.Rest().First()
}
func Third(seq Seq) Object {
return seq.Rest().Rest().First()
}
func Forth(seq Seq) Object {
return seq.Rest().Rest().Rest().First()
}
func ToSlice(seq Seq) []Object {
res := make([]Object, 0)
for !seq.IsEmpty() {
res = append(res, seq.First())
seq = seq.Rest()
}
return res
}
func SeqCount(seq Seq) int {
c := 0
for !seq.IsEmpty() {
switch obj := seq.(type) {
case Counted:
return c + obj.Count()
}
c++
seq = seq.Rest()
}
return c
}
func SeqNth(seq Seq, n int) Object {
if n < 0 {
panic(RT.NewError(fmt.Sprintf("Negative index: %d", n)))
}
i := n
for !seq.IsEmpty() {
if i == 0 {
return seq.First()
}
seq = seq.Rest()
i--
}
panic(RT.NewError(fmt.Sprintf("Index %d exceeds seq's length %d", n, (n - i))))
}
func SeqTryNth(seq Seq, n int, d Object) Object {
if n < 0 {
return d
}
i := n
for !seq.IsEmpty() {
if i == 0 {
return seq.First()
}
seq = seq.Rest()
i--
}
return d
}
func hashUnordered(seq Seq, seed uint32) uint32 {
for !seq.IsEmpty() {
seed += seq.First().Hash()
seq = seq.Rest()
}
h := getHash()
h.Write(uint32ToBytes(seed))
return h.Sum32()
}
func hashOrdered(seq Seq) uint32 {
h := getHash()
for !seq.IsEmpty() {
h.Write(uint32ToBytes(seq.First().Hash()))
seq = seq.Rest()
}
return h.Sum32()
}