forked from candid82/joker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
117 lines (92 loc) · 1.82 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
package core
type List struct {
InfoHolder
MetaHolder
first Object
rest *List
count int
}
func NewList(first Object, rest *List) *List {
result := List{
first: first,
rest: rest,
}
if rest != nil {
result.count = rest.count + 1
}
return &result
}
func NewListFrom(objs ...Object) *List {
res := EmptyList
for i := len(objs) - 1; i >= 0; i-- {
res = res.conj(objs[i])
}
return res
}
func (list *List) WithMeta(meta Map) Object {
res := *list
res.meta = SafeMerge(res.meta, meta)
return &res
}
func (list *List) conj(obj Object) *List {
return NewList(obj, list)
}
func (list *List) Conj(obj Object) Conjable {
return list.conj(obj)
}
func (list *List) ToString(escape bool) string {
return SeqToString(list, escape)
}
func (list *List) Equals(other interface{}) bool {
return IsSeqEqual(list, other)
}
func (list *List) GetType() *Type {
return TYPES["List"]
}
func (list *List) Hash() uint32 {
return hashOrdered(list)
}
func (list *List) First() Object {
return list.first
}
func (list *List) Rest() Seq {
return list.rest
}
func (list *List) IsEmpty() bool {
return list.count == 0
}
func (list *List) Cons(obj Object) Seq {
return list.conj(obj)
}
func (list *List) Seq() Seq {
return list
}
func (list *List) Second() Object {
return list.rest.first
}
func (list *List) Third() Object {
return list.rest.rest.first
}
func (list *List) Forth() Object {
return list.rest.rest.rest.first
}
func (list *List) Count() int {
return list.count
}
func (list *List) Empty() Collection {
return EmptyList
}
func (list *List) Peek() Object {
return list.first
}
func (list *List) Pop() Stack {
if list.count == 0 {
panic(RT.NewError("Can't pop empty list"))
}
return list.rest
}
func (list *List) sequential() {}
var EmptyList = NewList(Nil{}, nil)
func init() {
EmptyList.rest = EmptyList
}