This repository was archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterator.lua
More file actions
263 lines (239 loc) · 6.22 KB
/
Iterator.lua
File metadata and controls
263 lines (239 loc) · 6.22 KB
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
-- Library for manipulating key-value iterators.
local P = {}
Iterator = P
-- Copy the keys and values from the provided iterator into a table.
function P.totable(...)
local t = {}
for k, v in unpack(arg) do t[k] = v end
return t
end
-- Return a new iterator with the function fn(k, v) -> k2, v2 applied to all
-- key-value pairs. Any keys mapped to nil will be deleted.
function P.map(fn, ...)
local f, s, k = unpack(arg)
return function()
while true do
local v
k, v = f(s, k)
if k == nil then
return nil, nil
else
local k2, v2 = fn(k, v)
if k2 ~= nil then return k2, v2 end
end
end
end, nil, nil
end
-- Return a new iterator with the function fn(k, v) -> v2 applied to all
-- key-value pairs. Keys will be renumbered with positive, continguous integers.
-- Any keys mapped to nil will be deleted.
function P.imap(fn, ...)
local i = 0
local f, s, k = unpack(arg)
return function()
while true do
local v
k, v = f(s, k)
if k == nil then
return nil, nil
else
local v2 = fn(k, v)
if v2 ~= nil then
i = i + 1
return i, fn(k, v)
end
end
end
end, nil, nil
end
-- Return a new iterator with all key-value pairs filtered such that fn(k, v) is
-- true.
function P.filter(fn, ...)
local f, s, k = unpack(arg)
return function()
while true do
local v
k, v = f(s, k)
if k == nil then
return nil, nil
else
if fn(k, v) then return k, v end
end
end
end, nil, nil
end
-- Return a new iterator with all key-value pairs filtered such that fn(k, v) is
-- true. Keys will be renumbered with positive, continguous integers.
function P.ifilter(fn, ...)
local i = 0
local f, s, k = unpack(arg)
return function()
while true do
local v
k, v = f(s, k)
if k == nil then
return nil, nil
else
if fn(k, v) then
i = i + 1
return i, v
end
end
end
end, nil, nil
end
-- Combine the provided iterators into a single iterator. The iterators should
-- be supplied as function/invariant/value triplets packed into tables --
-- e.g., {pairs({ ... })} .
function P.concat(...)
if arg.n < 1 then return P.empty() end
local i = 1
local f, s, k = unpack(arg[i])
return function()
while true do
local v
k, v = f(s, k)
if k == nil then
i = i + 1
if arg[i] == nil then
return nil, nil
else
f, s, k = unpack(arg[i])
end
else
return k, v
end
end
end, nil, nil
end
--[[
Combine the provided iterators into a single iterator. Keys will be
renumbered with positive, continguous integers. The iterators should
be supplied as function/invariant/value triplets packed into tables --
e.g., {pairs({ ... })} .
]]
function P.iconcat(...)
if arg.n < 1 then return P.empty() end
local i = 1
local j = 0
local f, s, k = unpack(arg[i])
return function()
while true do
local v
k, v = f(s, k)
if k == nil then
i = i + 1
if arg[i] == nil then
return nil, nil
else
f, s, k = unpack(arg[i])
end
else
j = j + 1
return j, v
end
end
end, nil, nil
end
--[[
Combine the iterators yielded by the provided iterator (in the form of
function/invariant/value triplets packed into tables) into a single iterator.
Keys will be discarded.
]]
function P.chain(...)
if arg.n < 1 then return P.empty() end
local f, s, k = unpack(arg)
local v
local fi, si, ki = P.empty()
return function()
while true do
local vi
ki, vi = fi(si, ki)
if ki == nil then
k, v = f(s, k)
if k == nil then
return nil, nil
else
fi, si, ki = unpack(v)
end
else
return ki, vi
end
end
end, nil, nil
end
-- Returns true if fn(k, v) is true for one key-value pair in the iterator.
function P.hasone(fn, ...) return P.findfirst(fn, unpack(arg)) ~= nil end
-- Returns the key of the first key-value pair in the iterator such that
-- fn(k, v) is true.
function P.findfirst(fn, ...)
for k, v in unpack(arg) do if fn(k, v) then return k end end
return nil
end
-- Returns the key of the minimum key-value pair in the iterator given comp,
-- a comparison function that returns true if the first argument is less than
-- the second.
function P.min(comp, ...)
local mink = nil
local minv
for k, v in unpack(arg) do
if mink == nil or comp(v, minv) then mink, minv = k, v end
end
return mink
end
-- Returns the key of the maximum key-value pair in the iterator given comp,
-- a comparison function that returns true if the first argument is less than
-- the second.
function P.max(comp, ...)
local maxk = nil
local maxv
for k, v in unpack(arg) do
if maxk == nil or comp(maxv, v) then maxk, maxv = k, v end
end
return maxk
end
-- A simple a less than b comparer.
function P.ltcomp(a, b) return a < b end
local function getkey(k, _) return k end
-- Iterate through all of the keys in the provided key-value iterator.
function P.keys(...) return P.imap(getkey, unpack(arg)) end
local function getvalue(_, v) return v end
-- Iterate through all of the values in the provided key-value iterator.
function P.values(...) return P.imap(getvalue, unpack(arg)) end
-- Joins the values of the iterator together with the provided separator, like
-- table.concat(t, sep).
function P.join(sep, ...)
local f, s, v = unpack(arg)
local k
k, v = f(s, v)
if k == nil then return "" end
local res = tostring(v)
for _, v2 in f, s, v do res = res .. sep .. tostring(v2) end
return res
end
local function iterrange(s, v)
if v < s then
return v + 1, v + 1
else
return nil, nil
end
end
-- Produces a sequence of linear (x, x) pairs, where x ranges from 1 to n
-- inclusive.
function P.range(n) return iterrange, n, 0 end
-- An iterator that returns a single key-value pair.
function P.singleton(k, v)
local returned = false
return function()
if not returned then
returned = true
return k, v
else
return nil, nil
end
end, nil, nil
end
local empty = {}
-- An empty iterator.
function P.empty() return pairs(empty) end
return P