-
Notifications
You must be signed in to change notification settings - Fork 1
/
repeat.go
145 lines (122 loc) · 2.28 KB
/
repeat.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
package lang
type Repeat struct {
meta IPersistentMap
hash, hasheq uint32
x interface{}
count int64
next ISeq
}
var (
_ ASeq = (*Repeat)(nil)
_ ISeq = (*Repeat)(nil)
_ Sequential = (*Repeat)(nil)
_ IReduce = (*Repeat)(nil)
_ IReduceInit = (*Repeat)(nil)
)
func NewRepeat(x interface{}) *Repeat {
return &Repeat{x: x, count: -1}
}
func NewRepeatN(count int64, x interface{}) ISeq {
if count <= 0 {
return emptyList
}
return &Repeat{x: x, count: count}
}
func (r *Repeat) Meta() IPersistentMap {
return r.meta
}
func (r *Repeat) WithMeta(meta IPersistentMap) any {
if meta == r.meta {
return r
}
cpy := *r
cpy.meta = meta
return &cpy
}
func (r *Repeat) xxx_sequential() {}
func (r *Repeat) First() interface{} {
return r.x
}
func (r *Repeat) More() ISeq {
s := r.Next()
if s == nil {
return emptyList
}
return s
}
func (r *Repeat) Next() ISeq {
if r.next != nil {
return r.next
}
if r.count > 1 {
r.next = NewRepeatN(r.count-1, r.x)
} else if r.count == -1 {
r.next = r
}
return r.next
}
func (r *Repeat) Seq() ISeq {
return r
}
func (r *Repeat) Cons(val any) Conser {
return aseqCons(r, val)
}
func (r *Repeat) Count() int {
return aseqCount(r)
}
func (r *Repeat) Empty() IPersistentCollection {
return aseqEmpty()
}
func (r *Repeat) Equals(o any) bool {
return aseqEquals(r, o)
}
func (r *Repeat) Equiv(o any) bool {
return aseqEquiv(r, o)
}
func (r *Repeat) Hash() uint32 {
return aseqHash(&r.hash, r)
}
func (r *Repeat) HashEq() uint32 {
return aseqHashEq(&r.hasheq, r)
}
func (r *Repeat) String() string {
return aseqString(r)
}
func (r *Repeat) Reduce(f IFn) interface{} {
ret := r.x
if r.count == -1 {
for {
ret = f.Invoke(ret, r.x)
if IsReduced(ret) {
return ret.(IDeref).Deref()
}
}
} else {
for i := int64(1); i < r.count; i++ {
ret = f.Invoke(ret, r.x)
if IsReduced(ret) {
return ret.(IDeref).Deref()
}
}
return ret
}
}
func (r *Repeat) ReduceInit(f IFn, start interface{}) interface{} {
ret := start
if r.count == -1 {
for {
ret = f.Invoke(ret, r.x)
if IsReduced(ret) {
return ret.(IDeref).Deref()
}
}
} else {
for i := int64(0); i < r.count; i++ {
ret = f.Invoke(ret, r.x)
if IsReduced(ret) {
return ret.(IDeref).Deref()
}
}
return ret
}
}