forked from lua-stdlib/lua-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.lua
More file actions
514 lines (407 loc) · 12.2 KB
/
list.lua
File metadata and controls
514 lines (407 loc) · 12.2 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
--[[--
Tables as lists.
Prototype Chain
---------------
table
`-> Object
`-> List
@classmod std.list
]]
local base = require "std.base"
local debug = require "std.debug"
local Object = require "std.object" {}
local ipairs, pairs = base.ipairs, base.pairs
local len = base.len
local compare = base.compare
local prototype = base.prototype
local M, List
local function append (l, x)
local r = l {}
r[#r + 1] = x
return r
end
local function concat (l, ...)
local r = List {}
for _, e in ipairs {l, ...} do
for _, v in ipairs (e) do
r[#r + 1] = v
end
end
return r
end
local function rep (l, n)
local r = List {}
for i = 1, n do
r = concat (r, l)
end
return r
end
local function sub (l, from, to)
local r = List {}
local lenl = len (l)
from = from or 1
to = to or lenl
if from < 0 then
from = from + lenl + 1
end
if to < 0 then
to = to + lenl + 1
end
for i = from, to do
r[#r + 1] = l[i]
end
return r
end
--[[ ================= ]]--
--[[ Public Interface. ]]--
--[[ ================= ]]--
local function X (decl, fn)
return debug.argscheck ("std.list." .. decl, fn)
end
M = {
--- Append an item to a list.
-- @static
-- @function append
-- @tparam List l a list
-- @param x item
-- @treturn List new list with *x* appended
-- @usage
-- longer = append (short, "last")
append = X ("append (List, any)", append),
--- Compare two lists element-by-element, from left-to-right.
-- @static
-- @function compare
-- @tparam List l a list
-- @tparam List|table m another list, or table
-- @return -1 if *l* is less than *m*, 0 if they are the same, and 1
-- if *l* is greater than *m*
-- @usage
-- if a_list:compare (another_list) == 0 then print "same" end
compare = X ("compare (List, List|table)", compare),
--- Concatenate the elements from any number of lists.
-- @static
-- @function concat
-- @tparam List l a list
-- @param ... tuple of lists
-- @treturn List new list with elements from arguments
-- @usage
-- --> {1, 2, 3, {4, 5}, 6, 7}
-- list.concat ({1, 2, 3}, {{4, 5}, 6, 7})
concat = X ("concat (List, List|table*)", concat),
--- Prepend an item to a list.
-- @static
-- @function cons
-- @tparam List l a list
-- @param x item
-- @treturn List new list with *x* followed by elements of *l*
-- @usage
-- --> {"x", 1, 2, 3}
-- list.cons ({1, 2, 3}, "x")
cons = X ("cons (List, any)", function (l, x) return List {x, unpack (l)} end),
--- Repeat a list.
-- @static
-- @function rep
-- @tparam List l a list
-- @int n number of times to repeat
-- @treturn List *n* copies of *l* appended together
-- @usage
-- --> {1, 2, 3, 1, 2, 3, 1, 2, 3}
-- list.rep ({1, 2, 3}, 3)
rep = X ("rep (List, int)", rep),
--- Return a sub-range of a list.
-- (The equivalent of @{string.sub} on strings; negative list indices
-- count from the end of the list.)
-- @static
-- @function sub
-- @tparam List l a list
-- @int[opt=1] from start of range
-- @int[opt=#l] to end of range
-- @treturn List new list containing elements between *from* and *to*
-- inclusive
-- @usage
-- --> {3, 4, 5}
-- list.sub ({1, 2, 3, 4, 5, 6}, 3, 5)
sub = X ("sub (List, int?, int?)", sub),
--- Return a list with its first element removed.
-- @static
-- @function tail
-- @tparam List l a list
-- @treturn List new list with all but the first element of *l*
-- @usage
-- --> {3, {4, 5}, 6, 7}
-- list.tail {{1, 2}, 3, {4, 5}, 6, 7}
tail = X ("tail (List)", function (l) return sub (l, 2) end),
}
--[[ ============= ]]--
--[[ Deprecations. ]]--
--[[ ============= ]]--
-- This entire section can be deleted in due course, with just one
-- additional small correction noted in FIXME comments in the List
-- object constructor at the end of this file.
local DEPRECATED = debug.DEPRECATED
local function depair (ls)
local t = {}
for _, v in ipairs (ls) do
t[v[1]] = v[2]
end
return t
end
local function enpair (t)
local ls = List {}
for i, v in pairs (t) do
ls[#ls + 1] = List {i, v}
end
return ls
end
local function filter (pfn, l)
local r = List {}
for _, e in ipairs (l) do
if pfn (e) then
r[#r + 1] = e
end
end
return r
end
local function flatten (l)
local r = List {}
for v in base.leaves (ipairs, l) do
r[#r + 1] = v
end
return r
end
local function foldl (fn, d, t)
if t == nil then
local tail = {}
for i = 2, len (d) do tail[#tail + 1] = d[i] end
d, t = d[1], tail
end
return base.reduce (fn, d, ipairs, t)
end
local function foldr (fn, d, t)
if t == nil then
local u, last = {}, len (d)
for i = 1, last - 1 do u[#u + 1] = d[i] end
d, t = d[last], u
end
return base.reduce (
function (x, y) return fn (y, x) end, d, ipairs, base.ireverse (t))
end
local function index_key (f, l)
local r = {}
for i, v in ipairs (l) do
local k = v[f]
if k then
r[k] = i
end
end
return r
end
local function index_value (f, l)
local r = {}
for i, v in ipairs (l) do
local k = v[f]
if k then
r[k] = v
end
end
return r
end
local function map (fn, l)
local r = List {}
for _, e in ipairs (l) do
local v = fn (e)
if v ~= nil then
r[#r + 1] = v
end
end
return r
end
local function map_with (fn, ls)
return map (function (...) return fn (unpack (...)) end, ls)
end
local function project (x, l)
return map (function (t) return t[x] end, l)
end
local function relems (l) return base.ielems (base.ireverse (l)) end
local function reverse (l) return List (base.ireverse (l)) end
local function shape (s, l)
l = flatten (l)
-- Check the shape and calculate the size of the zero, if any
local size = 1
local zero
for i, v in ipairs (s) do
if v == 0 then
if zero then -- bad shape: two zeros
return nil
else
zero = i
end
else
size = size * v
end
end
if zero then
s[zero] = math.ceil (len (l) / size)
end
local function fill (i, d)
if d > len (s) then
return l[i], i + 1
else
local r = List {}
for j = 1, s[d] do
local e
e, i = fill (i, d + 1)
r[#r + 1] = e
end
return r, i
end
end
return (fill (1, 1))
end
local function transpose (ls)
local rs, lenls, dims = List {}, len (ls), map (len, ls)
if len (dims) > 0 then
for i = 1, math.max (unpack (dims)) do
rs[i] = List {}
for j = 1, lenls do
rs[i][j] = ls[j][i]
end
end
end
return rs
end
local function zip_with (ls, fn)
return map_with (fn, transpose (ls))
end
local m = {
append = M.append,
compare = M.compare,
concat = M.concat,
cons = M.cons,
rep = M.rep,
sub = M.sub,
tail = M.tail,
}
m.depair = DEPRECATED ("38", "'std.list:depair'", depair)
m.map_with = DEPRECATED ("38", "'std.list:map_with'",
function (self, fn) return map_with (fn, self) end)
m.transpose = DEPRECATED ("38", "'std.list:transpose'", transpose)
m.zip_with = DEPRECATED ("38", "'std.list:zip_with'", zip_with)
M.depair = DEPRECATED ("41", "'std.list.depair'", depair)
M.enpair = DEPRECATED ("41", "'std.list.enpair'", enpair)
m.enpair = DEPRECATED ("41", "'std.list:enpair'", enpair)
M.elems = DEPRECATED ("41", "'std.list.elems'",
"use 'std.ielems' instead", base.ielems)
m.elems = DEPRECATED ("41", "'std.list:elems'",
"use 'std.ielems' instead", base.ielems)
M.filter = DEPRECATED ("41", "'std.list.filter'",
"use 'std.functional.filter' instead", filter)
m.filter = DEPRECATED ("41", "'std.list:filter'",
"use 'std.functional.filter' instead",
function (self, p) return filter (p, self) end)
M.flatten = DEPRECATED ("41", "'std.list.flatten'",
"use 'std.functional.flatten' instead", flatten)
m.flatten = DEPRECATED ("41", "'std.list:flatten'",
"use 'std.functional.flatten' instead", flatten)
M.foldl = DEPRECATED ("41", "'std.list.foldl'",
"use 'std.functional.foldl' instead", foldl)
m.foldl = DEPRECATED ("41", "'std.list:foldl'",
"use 'std.functional.foldl' instead",
function (self, fn, e)
if e ~= nil then return foldl (fn, e, self) end
return foldl (fn, self)
end)
M.foldr = DEPRECATED ("41", "'std.list.foldr'",
"use 'std.functional.foldr' instead", foldr)
m.foldr = DEPRECATED ("41", "'std.list:foldr'",
"use 'std.functional.foldr' instead",
function (self, fn, e)
if e ~= nil then return foldr (fn, e, self) end
return foldr (fn, self)
end)
M.index_key = DEPRECATED ("41", "'std.list.index_key'",
"compose 'std.functional.filter' and 'std.table.invert' instead",
index_key)
m.index_key = DEPRECATED ("41", "'std.list:index_key'",
function (self, fn) return index_key (fn, self) end)
M.index_value = DEPRECATED ("41", "'std.list.index_value'",
"compose 'std.functional.filter' and 'std.table.invert' instead",
index_value)
m.index_value = DEPRECATED ("41", "'std.list:index_value'",
function (self, fn) return index_value (fn, self) end)
M.map = DEPRECATED ("41", "'std.list.map'",
"use 'std.functional.map' instead", map)
m.map = DEPRECATED ("41", "'std.list:map'",
"use 'std.functional.map' instead",
function (self, fn) return map (fn, self) end)
M.map_with = DEPRECATED ("41", "'std.list.map_with'",
"use 'std.functional.map_with' instead", map_with)
M.project = DEPRECATED ("41", "'std.list.project'",
"use 'std.table.project' instead", project)
m.project = DEPRECATED ("41", "'std.list:project'",
"use 'std.table.project' instead",
function (self, x) return project (x, self) end)
M.relems = DEPRECATED ("41", "'std.list.relems'",
"compose 'std.ielems' and 'std.ireverse' instead", relems)
m.relems = DEPRECATED ("41", "'std.list:relems'", relems)
M.reverse = DEPRECATED ("41", "'std.list.reverse'",
"use 'std.ireverse' instead", reverse)
m.reverse = DEPRECATED ("41", "'std.list:reverse'",
"use 'std.ireverse' instead", reverse)
M.shape = DEPRECATED ("41", "'std.list.shape'",
"use 'std.table.shape' instead", shape)
m.shape = DEPRECATED ("41", "'std.list:shape'",
"use 'std.table.shape' instead",
function (t, l) return shape (l, t) end)
M.transpose = DEPRECATED ("41", "'std.list.transpose'",
"use 'std.functional.zip' instead", transpose)
M.zip_with = DEPRECATED ("41", "'std.list.zip_with'",
"use 'std.functional.zip_with' instead", zip_with)
--[[ ================== ]]--
--[[ Type Declarations. ]]--
--[[ ================== ]]--
--- An Object derived List.
-- @object List
List = Object {
-- Derived object type.
_type = "List",
_functions = M, -- FIXME: remove this when DEPRECATIONS have gone
__index = m, -- FIXME: `__index = M` when DEPRECATIONS have gone
------
-- Concatenate lists.
-- @function __concat
-- @tparam List l a list
-- @tparam List|table m another list, or table (hash part is ignored)
-- @see concat
-- @usage
-- new = alist .. {"append", "these", "elements"}
__concat = concat,
------
-- Append element to list.
-- @function __add
-- @tparam List l a list
-- @param e element to append
-- @see append
-- @usage
-- list = list + "element"
__add = append,
------
-- List order operator.
-- @function __lt
-- @tparam List l a list
-- @tparam List m another list
-- @see compare
-- @usage
-- max = list1 > list2 and list1 or list2
__lt = function (list1, list2) return compare (list1, list2) < 0 end,
------
-- List equality or order operator.
-- @function __le
-- @tparam List l a list
-- @tparam List m another list
-- @see compare
-- @usage
-- min = list1 <= list2 and list1 or list2
__le = function (list1, list2) return compare (list1, list2) <= 0 end,
}
return List