-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
294 lines (238 loc) · 4.78 KB
/
list.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
package lang
import (
"reflect"
"github.com/glojurelang/glojure/internal/murmur3"
)
// List is a list of values.
type List struct {
meta IPersistentMap
hash uint32
hashEq uint32
// the empty list is represented by a nil item and a nil next. all
// other lists have a non-nil item and a non-nil next.
item any
next *List
size int
}
var (
_ ASeq = (*List)(nil)
_ IObj = (*List)(nil)
_ ISeq = (*List)(nil)
_ IPersistentList = (*List)(nil)
_ Counted = (*List)(nil)
_ IReduce = (*List)(nil)
_ IReduceInit = (*List)(nil)
)
type EmptyList struct {
meta IPersistentMap
}
var (
_ IObj = (*EmptyList)(nil)
_ ISeq = (*EmptyList)(nil)
_ IPersistentList = (*EmptyList)(nil)
_ Counted = (*EmptyList)(nil)
_ IReduce = (*EmptyList)(nil)
_ IReduceInit = (*EmptyList)(nil)
_ IHashEq = (*EmptyList)(nil)
)
func (e *EmptyList) xxx_sequential() {}
func (e *EmptyList) Cons(x any) Conser {
return NewList(x)
}
func (e *EmptyList) Count() int {
return 0
}
func (e *EmptyList) Peek() any {
return nil
}
func (e *EmptyList) Pop() IPersistentStack {
panic("cannot pop empty list")
}
func (e *EmptyList) Seq() ISeq {
return nil
}
func (e *EmptyList) First() any {
return nil
}
func (e *EmptyList) Next() ISeq {
return nil
}
func (e *EmptyList) More() ISeq {
return e
}
func (e *EmptyList) IsEmpty() bool {
return true
}
func (e *EmptyList) Empty() IPersistentCollection {
return e
}
func (e *EmptyList) Equals(other any) bool {
if e == other {
return true
}
if _, ok := other.(Sequential); ok {
return Seq(other) == nil
}
t := reflect.TypeOf(other)
if t != nil && t.Kind() == reflect.Slice {
return Seq(other) == nil
}
return false
}
func (e *EmptyList) Equiv(other any) bool {
return e.Equals(other)
}
func (e *EmptyList) Meta() IPersistentMap {
return e.meta
}
func (e *EmptyList) Hash() uint32 {
return 1
}
var (
emptyHashOrdered = murmur3.HashOrdered(nil, HashEq)
)
func (e *EmptyList) HashEq() uint32 {
return emptyHashOrdered
}
func (e *EmptyList) WithMeta(meta IPersistentMap) any {
if e.meta == meta {
return e
}
cpy := *e
cpy.meta = meta
return &cpy
}
func (e *EmptyList) String() string {
return "()"
}
func (e *EmptyList) Reduce(f IFn) any {
return f.Invoke()
}
func (e *EmptyList) ReduceInit(f IFn, init any) any {
return init
}
var emptyList = &EmptyList{}
////////////////////////////////////////////////////////////////////////////////
func NewList(values ...any) IPersistentList {
if len(values) == 0 {
return &EmptyList{}
}
var list *List
size := 0
for i := len(values) - 1; i >= 0; i-- {
size++
list = &List{
item: values[i],
next: list,
size: size,
}
}
return list
}
func ConsList(item any, next *List) *List {
size := 1
if next != nil {
size += next.size
}
return &List{
item: item,
next: next,
size: size,
}
}
func (l *List) xxx_sequential() {}
func (l *List) First() any {
return l.Item()
}
// Item returns the data from this list node. AKA car.
func (l *List) Item() any {
return l.item
}
func (l *List) Seq() ISeq {
return l
}
// Next returns the next list node. AKA cdr, with the requirement that
// it must be a list.
func (l *List) Next() ISeq {
if l.IsEmpty() || l.Count() == 1 {
return nil
}
return l.next
}
func (l *List) More() ISeq {
s := l.Next()
if s == nil {
return emptyList
}
return s
}
func (l *List) IsEmpty() bool {
return false
}
func (l *List) Empty() IPersistentCollection {
return emptyList.WithMeta(l.meta).(IPersistentCollection)
}
func (l *List) Count() int {
return l.size
}
func (l *List) Cons(x any) Conser {
return ConsList(x, l)
}
func (l *List) String() string {
return PrintString(l)
}
func (l *List) Reduce(f IFn) any {
ret := l.First()
for s := l.Next(); s != nil; s = s.Next() {
ret = f.Invoke(ret, s.First())
if IsReduced(ret) {
return ret.(IDeref).Deref()
}
}
return ret
}
func (l *List) ReduceInit(f IFn, init any) any {
ret := f.Invoke(init, l.First())
for s := l.Next(); s != nil; s = s.Next() {
if IsReduced(ret) {
return ret.(IDeref).Deref()
}
ret = f.Invoke(ret, s.First())
}
if IsReduced(ret) {
return ret.(IDeref).Deref()
}
return ret
}
func (l *List) Meta() IPersistentMap {
return l.meta
}
func (l *List) WithMeta(meta IPersistentMap) any {
if l.meta == meta {
return l
}
cpy := *l
cpy.meta = meta
return &cpy
}
func (l *List) Peek() any {
return l.Item()
}
func (l *List) Pop() IPersistentStack {
if l.next == nil {
return emptyList
}
return l.next
}
func (l *List) Equals(other any) bool {
return aseqEquals(l, other)
}
func (l *List) Equiv(other any) bool {
return aseqEquiv(l, other)
}
func (l *List) Hash() uint32 {
return aseqHash(&l.hash, l)
}
func (l *List) HashEq() uint32 {
return aseqHashEq(&l.hashEq, l)
}