-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharray.lua
More file actions
164 lines (144 loc) · 3.02 KB
/
array.lua
File metadata and controls
164 lines (144 loc) · 3.02 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
Array = {}
Array.__index = Array
setmetatable(Array, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function Array.new(a)
a = a or {}
local self = setmetatable(a, Array)
return self
end
local function cloneElt(elt)
if type(elt) == "table" and elt.clone ~= nil then
return elt:clone()
else
return elt
end
end
function Array:clone()
local a = {}
for _, elt in ipairs(self) do
table.insert(a, cloneElt(elt))
end
return Array(a)
end
function Array:tostring()
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
return dump(self)
end
function Array:push_head(elt)
local res = self:clone()
table.insert(res, 1, elt)
return res
end
function Array:pop_head(elt)
local a = self:clone()
local head = table.remove(a, 1)
return head, a
end
function Array:push_tail(elt)
local res = self:clone()
table.insert(res, elt)
return res
end
function Array:pop_tail(elt)
local a = self:clone()
local tail = table.remove(a)
return a, tail
end
function Array:map(f)
local a = {}
for k,v in pairs(self) do
a[k] = f(v)
end
return Array(a)
end
function Array:find(f)
for i, v in ipairs(self) do
if f(v) then
return v
end
end
return nil
end
function Array:sort(f)
local sorted = self:clone()
table.sort(sorted, f)
return sorted
end
function Array:zip(tbl)
local new_tbl = {}
local size = math.max(#self, #tbl)
for i=1, size do
local one = cloneElt(self[i])
local two = cloneElt(tbl[i])
new_tbl[i] = {one, two}
end
return Array(new_tbl)
end
function Array:add_to_values(value)
return self:map(function(item) return item + value end)
end
function Array:rotate_values(rotation)
local new_table = {}
local size = #self
for i=1, size do
local pos = (i - 1 + rotation) % size + 1
local elt = cloneElt(self[pos])
table.insert(new_table, elt)
end
return Array(new_table)
end
function Array:diff_values(table)
return self
:zip(table)
:map(function(e) return e[1] - e[2] end)
end
function Array:fold_left(acc, f)
function do_fold(acc, f, i, size)
if i > size then
return acc
else
return do_fold(f(acc, self[i]), f, i + 1, size)
end
end
return do_fold(acc, f, 1, #self)
end
function Array:acc_table_values()
local f = function(acc, elt)
local size = #acc
local val = elt
if size ~= 0 then
val = val + acc[size]
end
table.insert(acc, val)
return acc
end
return self:fold_left(Array{}, f)
end
function Array:equals(array)
local equal_values = self:zip(array):fold_left(true, function(acc, e) return acc and (e[1] == e[2]) end)
return getmetatable(array) == Array
and #self == #array
and equal_values
end
function Array:sum()
local total = 0
for _, value in ipairs(self) do
total = total + value
end
return total
end