aboutsummaryrefslogtreecommitdiff
path: root/utils.lua
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-01-25 17:34:49 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2018-01-25 17:36:05 -0800
commitc79bea44016daf43f05300b772011b14125fa0df (patch)
tree10dbbc3bef495662be829b73893660622bd2d2c1 /utils.lua
parentf769351556cceed58ab6bf844c671114ec1862c2 (diff)
Overhaul of compiling API (eliminated some of the expr/statements
helpers and forced the use of {expr=..., locals=...}-type syntax. This helped fix up all of the cases like loops where locals were being mishandled and led to some cleaner code.
Diffstat (limited to 'utils.lua')
-rw-r--r--utils.lua27
1 files changed, 21 insertions, 6 deletions
diff --git a/utils.lua b/utils.lua
index 27951ea..80585a5 100644
--- a/utils.lua
+++ b/utils.lua
@@ -116,12 +116,15 @@ local function split(str, sep)
end
local function remove_from_list(list, item)
- for i=1,#list do
+ local deleted, N = 0, #list
+ for i=1,N do
if list[i] == item then
- table.remove(list, i)
- return
+ deleted = deleted + 1
+ else
+ list[i-deleted] = list[i]
end
end
+ for i=N-deleted+1,N do list[i] = nil end
end
local function accumulate(glue, co)
@@ -163,6 +166,18 @@ local function set(list)
return ret
end
+local function deduplicate(list)
+ local seen, deleted = {}, 0
+ for i, item in ipairs(list) do
+ if seen[item] then
+ deleted = deleted + 1
+ else
+ seen[item] = true
+ list[i-deleted] = list[i]
+ end
+ end
+end
+
local function sum(t)
local tot = 0
for i=1,#t do
@@ -343,6 +358,6 @@ end
return {is_list=is_list, size=size, repr=repr, stringify=stringify, split=split,
remove_from_list=remove_from_list, accumulate=accumulate, nth_to_last=nth_to_last,
- keys=keys, values=values, set=set, sum=sum, product=product, all=all, any=any,
- min=min, max=max, sort=sort, equivalent=equivalent, key_for=key_for, clamp=clamp,
- mix=mix, round=round}
+ keys=keys, values=values, set=set, deduplicate=deduplicate, sum=sum, product=product,
+ all=all, any=any, min=min, max=max, sort=sort, equivalent=equivalent, key_for=key_for,
+ clamp=clamp, mix=mix, round=round}