From e6a71ac13f976b0fe123b64fd2cb59047797d599 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sat, 13 Nov 2021 16:29:00 +0100 Subject: [PATCH] Add more small table utils --- table.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/table.lua b/table.lua index 2a8ecb2..6204669 100644 --- a/table.lua +++ b/table.lua @@ -5,6 +5,9 @@ local assert, ipairs, math, next, pairs, rawget, rawset, setmetatable, string, t local _ENV = {} setfenv(1, _ENV) +-- Empty table +empty = {} + -- Table helpers function map_index(table, func) @@ -391,6 +394,8 @@ function map(table, func) return table end +map_values = map + function map_keys(table, func) local new_tab = {} for key, value in pairs(table) do @@ -616,6 +621,16 @@ end 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) local len = #table for index = 1, math.floor(len / 2) do