mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 09:13:53 +01:00
Code quality improvements
This commit is contained in:
parent
9b72be42af
commit
6b8fb8da91
29
table.lua
29
table.lua
@ -1,5 +1,5 @@
|
|||||||
-- Localize globals
|
-- Localize globals
|
||||||
local assert, error, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, table, type = assert, error, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, table, type
|
local assert, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, table, type = assert, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, table, type
|
||||||
|
|
||||||
-- Set environment
|
-- Set environment
|
||||||
local _ENV = {}
|
local _ENV = {}
|
||||||
@ -58,7 +58,7 @@ local rope_len_metatable = {__index = {
|
|||||||
self.len = self.len + text:len()
|
self.len = self.len + text:len()
|
||||||
end
|
end
|
||||||
}}
|
}}
|
||||||
--> rope for determining length supporting :write(text), .len being the length
|
--> rope for determining length of text supporting `:write(text)` and `.len` to get the length of written text
|
||||||
function rope_len(len)
|
function rope_len(len)
|
||||||
return setmetatable({len = len or 0}, rope_len_metatable)
|
return setmetatable({len = len or 0}, rope_len_metatable)
|
||||||
end
|
end
|
||||||
@ -419,7 +419,6 @@ function find(list, value)
|
|||||||
return index
|
return index
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
contains = find
|
contains = find
|
||||||
@ -560,21 +559,20 @@ function rpairs(table)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function best_value(table, is_better_func)
|
function best_value(table, is_better_func)
|
||||||
local best = next(table)
|
local best_key = next(table)
|
||||||
if best == nil then
|
if best_key == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local candidate = best
|
local candidate_key = best_key
|
||||||
while true do
|
while true do
|
||||||
candidate = next(table, candidate)
|
candidate_key = next(table, candidate_key)
|
||||||
if candidate == nil then
|
if candidate_key == nil then
|
||||||
return best
|
return best_key
|
||||||
end
|
end
|
||||||
if is_better_func(candidate, best) then
|
if is_better_func(candidate_key, best_key) then
|
||||||
best = candidate
|
best_key = candidate_key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
error()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function min(table)
|
function min(table)
|
||||||
@ -619,9 +617,10 @@ end
|
|||||||
binary_search = binary_search_comparator(default_comparator)
|
binary_search = binary_search_comparator(default_comparator)
|
||||||
|
|
||||||
function reverse(table)
|
function reverse(table)
|
||||||
local l = #table + 1
|
local len = #table
|
||||||
for index = 1, math.floor(#table / 2) do
|
for index = 1, math.floor(len / 2) do
|
||||||
table[l - index], table[index] = table[index], table[l - index]
|
local index_from_end = len + 1 - index
|
||||||
|
table[index_from_end], table[index] = table[index], table[index_from_end]
|
||||||
end
|
end
|
||||||
return table
|
return table
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user