From b912582199e1c05db7b633fe08d00accea00f98c Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Tue, 6 Oct 2020 19:13:25 +0200 Subject: [PATCH] Fix table.binary_search, add table.map_keys --- table.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/table.lua b/table.lua index fede240..897aa6f 100644 --- a/table.lua +++ b/table.lua @@ -148,6 +148,14 @@ function map(t, func) return t end +function map_keys(tab, func) + local new_tab = {} + for key, value in pairs(tab) do + new_tab[func(key)] = value + end + return new_tab +end + function process(t, func) local r={} for k, v in pairs(t) do @@ -315,7 +323,7 @@ end function binary_search_comparator(comparator) -- if found, returns index; if not found, returns -index for insertion - function binary_search(list, value) + return function(list, value) local min, max = 1, #list while min <= max do local pivot = min + math.floor((max-min)/2)