-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListZip.roc
233 lines (157 loc) · 5.04 KB
/
ListZip.roc
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
interface ListZip exposes
[ Zip
, newAtFirst, newAtLast, newAt
, first, beforeFirst, last, afterLast
, forward, backward, move, moveTo
, collect
, Queue
, newFromTo, newStartEnd, startZip, endZip
, updateQueue, queueSize
, forwardStart, backwardStart
, forwardEnd, backwardEnd
, collectQueue
]
imports []
# ---
Zip : { len: I64, idx: I64, val: I64, default: I64 }
newAtFirst : List I64, I64 -> Zip
newAtFirst = \list, default ->
newAt list 0 default
newAtLast : List I64, I64 -> Zip
newAtLast = \list, default ->
lastIdx = List.len list - 1
newAt list lastIdx default
newAt : List I64, I64, I64 -> Zip
newAt = \list, idx, default ->
len = List.len list
val = read list idx default
{ len, idx, val, default }
first : Zip, List I64 -> Zip
first = \zip, list ->
moveTo zip list 0
beforeFirst : Zip -> Bool
beforeFirst = \zip ->
zip.idx < 0
last : Zip, List I64 -> Zip
last = \zip, list ->
moveTo zip list (zip.len - 1)
afterLast : Zip -> Bool
afterLast = \zip ->
zip.idx >= zip.len
forward : Zip, List I64 -> Zip
forward = \zip, list ->
move zip list 1
backward : Zip, List I64 -> Zip
backward = \zip, list ->
move zip list -1
move : Zip, List I64, I64 -> Zip
move = \zip, list, len ->
idx = zip.idx + len
moveTo zip list idx
moveTo : Zip, List I64, I64 -> Zip
moveTo = \zip, list, idx ->
if idx == zip.idx then
zip
else
val = read list idx zip.default
{ zip & idx, val }
read : List I64, I64, I64 -> I64
read = \list, idx, default ->
when List.get list idx is
Ok n -> n
_ -> default
collect : List I64, Zip, I64 -> List I64
collect = \list, zip, len ->
collectHelper list zip len []
collectHelper : List I64, Zip, I64, List I64 -> List I64
collectHelper = \list, zip, len, acc ->
if len > 1 then
newAcc = List.append acc zip.val
newZip = forward zip list
newLen = len - 1
collectHelper list newZip newLen newAcc
else if len == 1 then
List.append acc zip.val
else
acc
# ---
Queue : { len: I64, startIdx: I64, startVal: I64, endIdx: I64, endVal: I64, default: I64 }
newFromTo : List I64, I64, I64, I64 -> Queue
newFromTo = \list, startIdx, endIdx, default ->
len = List.len list
startVal = read list startIdx default
endVal = read list endIdx default
{ len, startIdx, startVal, endIdx, endVal, default }
newStartEnd : Zip, Zip, I64 -> Queue
newStartEnd = \start, end, default ->
{ len: end.len, startIdx: start.idx, startVal: start.val, endIdx: end.idx, endVal: end.val, default }
startZip : Queue -> Zip
startZip = \queue ->
{ len: queue.len, idx: queue.startIdx, val: queue.startVal, default: queue.default }
endZip : Queue -> Zip
endZip = \queue ->
{ len: queue.len, idx: queue.endIdx, val: queue.endVal, default: queue.default }
forwardStart : Queue, List I64 -> Queue
forwardStart = \queue, list ->
moveStart queue list 1
backwardStart : Queue, List I64 -> Queue
backwardStart = \queue, list ->
moveStart queue list -1
moveStart : Queue, List I64, I64 -> Queue
moveStart = \queue, list, len ->
startIdx = checkedIdx queue (queue.startIdx + len)
moveStartTo queue list startIdx
moveStartTo : Queue, List I64, I64 -> Queue
moveStartTo = \queue, list, startIdx ->
if startIdx == queue.startIdx then
queue
else
startVal = read list startIdx queue.default
{ queue & startIdx, startVal }
forwardEnd : Queue, List I64 -> Queue
forwardEnd = \queue, list ->
moveEnd queue list 1
backwardEnd : Queue, List I64 -> Queue
backwardEnd = \queue, list ->
moveEnd queue list -1
moveEnd : Queue, List I64, I64 -> Queue
moveEnd = \queue, list, len ->
endIdx = checkedIdx queue (queue.endIdx + len)
moveEndTo queue list endIdx
moveEndTo : Queue, List I64, I64 -> Queue
moveEndTo = \queue, list, endIdx ->
if endIdx == queue.endIdx then
queue
else
endVal = read list endIdx queue.default
{ queue & endIdx, endVal }
queueSize : Queue -> I64
queueSize = \queue ->
checkedIdx queue (queue.endIdx - queue.startIdx + 1)
checkedIdx : Queue, I64 -> I64
checkedIdx = \queue, idx ->
if idx < 0 then
checkedIdx queue (idx + queue.len)
else if idx >= queue.len then
checkedIdx queue (idx - queue.len)
else
idx
collectQueue : List I64, Queue, I64 -> List I64
collectQueue = \list, queue, len ->
collectQueueHelper list queue len []
collectQueueHelper : List I64, Queue, I64, List I64 -> List I64
collectQueueHelper = \list, queue, len, acc ->
if len > 1 then
newAcc = List.append acc queue.startVal
newQueue = forwardStart queue list
newLen = len - 1
collectQueueHelper list newQueue newLen newAcc
else if len == 1 then
List.append acc queue.startVal
else
acc
updateQueue : Queue, List I64 -> Queue
updateQueue = \queue, list ->
startVal = read list queue.startIdx queue.default
endVal = read list queue.endIdx queue.default
{ queue & startVal, endVal }