mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 09:13:53 +01:00
Add more small table utils
This commit is contained in:
parent
6b8fb8da91
commit
e6a71ac13f
15
table.lua
15
table.lua
@ -5,6 +5,9 @@ local assert, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, t
|
|||||||
local _ENV = {}
|
local _ENV = {}
|
||||||
setfenv(1, _ENV)
|
setfenv(1, _ENV)
|
||||||
|
|
||||||
|
-- Empty table
|
||||||
|
empty = {}
|
||||||
|
|
||||||
-- Table helpers
|
-- Table helpers
|
||||||
|
|
||||||
function map_index(table, func)
|
function map_index(table, func)
|
||||||
@ -391,6 +394,8 @@ function map(table, func)
|
|||||||
return table
|
return table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
map_values = map
|
||||||
|
|
||||||
function map_keys(table, func)
|
function map_keys(table, func)
|
||||||
local new_tab = {}
|
local new_tab = {}
|
||||||
for key, value in pairs(table) do
|
for key, value in pairs(table) do
|
||||||
@ -616,6 +621,16 @@ end
|
|||||||
|
|
||||||
binary_search = binary_search_comparator(default_comparator)
|
binary_search = binary_search_comparator(default_comparator)
|
||||||
|
|
||||||
|
--> whether the list is sorted in ascending order
|
||||||
|
function is_sorted(list, comparator)
|
||||||
|
for index = 2, #list do
|
||||||
|
if comparator(list[index - 1], list[index]) >= 0 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
function reverse(table)
|
function reverse(table)
|
||||||
local len = #table
|
local len = #table
|
||||||
for index = 1, math.floor(len / 2) do
|
for index = 1, math.floor(len / 2) do
|
||||||
|
Loading…
Reference in New Issue
Block a user