From c79bea44016daf43f05300b772011b14125fa0df Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 25 Jan 2018 17:34:49 -0800 Subject: 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. --- utils.lua | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'utils.lua') 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} -- cgit v1.2.3