-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.lua
executable file
·123 lines (93 loc) · 2.78 KB
/
list.lua
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
#!/usr/bin/env lua
-- Copyright © 2025 Mark Summerfield. All rights reserved.
local ok, lx = pcall(require, "lx.lx")
if not ok then lx = require("lx") end
local List
local methods = {}
function methods.typeof() return "List" end
function methods:isempty() return next(self.values_) == nil end
function methods:len() return #self.values_ end
function methods:first()
return #self.values_ > 0 and self.values_[1] or nil
end
function methods:last()
return #self.values_ > 0 and self.values_[#self.values_] or nil
end
function methods:at(pos)
return pos <= #self.values_ and self.values_[pos] or nil
end
function methods:find(value)
for i, x in ipairs(self.values_) do
if x == value then return i end
end
return nil
end
function methods:rfind(value)
for i = #self.values_, 1, -1 do
if self.values_[i] == value then return i end
end
return nil
end
function methods:contains(value) return self:find(value) ~= nil end
function methods:sort(cmp) table.sort(self.values_, cmp) end
function methods:random_value()
return #self.values_ > 0
and self.values_[math.random(1, #self.values_)]
or nil
end
function methods:append(...)
for _, value in ipairs({ ... }) do
table.insert(self.values_, value)
end
end
function methods:append_list(list)
for _, value in ipairs(list.values_) do
table.insert(self.values_, value)
end
end
function methods:set(pos, value) self.values_[pos] = value end
function methods:insert(pos, value) table.insert(self.values_, pos, value) end
function methods:pop()
return #self.values_ > 0 and self:remove(#self.values_) or nil
end
function methods:remove(pos)
if pos > #self.values_ then return nil end
local value = self.values_[pos]
table.remove(self.values_, pos)
return value
end
function methods:tostring()
local strs = {}
for _, value in ipairs(self.values_) do
table.insert(strs, lx.dump(value))
end
return "[" .. table.concat(strs, " ") .. "]"
end
function methods:clear() self.values_ = {} end
-- values = list:values()
-- for _, value in ipairs(values) do ... end -- is faster than list:iter()
function methods:iter()
local i = 0
return function()
i = i + 1
return self.values_[i]
end
end
function methods:values() return self.values_ end
local meta = { __index = methods }
function meta:__tostring() return self:tostring() end
function meta:__len() return #self.values_ end
function meta:__eq(list)
if #self.values_ ~= #list then return false end
for i = 1, #self.values_ do
if self.values_[i] ~= list.values_[i] then return false end
end
return true
end
List = function(...)
local self = { values_ = {} }
setmetatable(self, meta)
self:append(...)
return self
end
return List