From 0672998fb8303d86ee3efaea7c8084ba5e01d563 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 17 May 2013 08:30:28 +0100 Subject: util.iterators: Various fixes and improvements, primarily use pack() where it should be used. --- util/iterators.lua | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'util/iterators.lua') diff --git a/util/iterators.lua b/util/iterators.lua index 1f6aacb8..8f521fc4 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -10,6 +10,10 @@ local it = {}; +local t_insert = table.insert; +local select, unpack, next = select, unpack, next; +local function pack(...) return { n = select("#", ...), ... }; end + -- Reverse an iterator function it.reverse(f, s, var) local results = {}; @@ -19,7 +23,7 @@ function it.reverse(f, s, var) local ret = { f(s, var) }; var = ret[1]; if var == nil then break; end - table.insert(results, 1, ret); + t_insert(results, 1, ret); end -- Then return our reverse one @@ -55,12 +59,12 @@ function it.unique(f, s, var) return function () while true do - local ret = { f(s, var) }; + local ret = pack(f(s, var)); var = ret[1]; if var == nil then break; end if not set[var] then set[var] = true; - return var; + return unpack(ret, 1, ret.n); end end end; @@ -71,8 +75,7 @@ function it.count(f, s, var) local x = 0; while true do - local ret = { f(s, var) }; - var = ret[1]; + local var = f(s, var); if var == nil then break; end x = x + 1; end @@ -104,7 +107,7 @@ end function it.tail(n, f, s, var) local results, count = {}, 0; while true do - local ret = { f(s, var) }; + local ret = pack(f(s, var)); var = ret[1]; if var == nil then break; end results[(count%n)+1] = ret; @@ -117,7 +120,8 @@ function it.tail(n, f, s, var) return function () pos = pos + 1; if pos > n then return nil; end - return unpack(results[((count-1+pos)%n)+1]); + local ret = results[((count-1+pos)%n)+1]; + return unpack(ret, 1, ret.n); end --return reverse(head(n, reverse(f, s, var))); end @@ -139,7 +143,7 @@ function it.to_array(f, s, var) while true do var = f(s, var); if var == nil then break; end - table.insert(t, var); + t_insert(t, var); end return t; end -- cgit v1.2.3 From 04ec0856a81726f5e0f67dc3c8e34c5f5027e06e Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 17 May 2013 08:31:03 +0100 Subject: util.iterators: Add filter() to run results through a filter function --- util/iterators.lua | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'util/iterators.lua') diff --git a/util/iterators.lua b/util/iterators.lua index 8f521fc4..bcda48b4 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -123,7 +123,21 @@ function it.tail(n, f, s, var) local ret = results[((count-1+pos)%n)+1]; return unpack(ret, 1, ret.n); end - --return reverse(head(n, reverse(f, s, var))); + --return reverse(head(n, reverse(f, s, var))); -- ! +end + +function it.filter(filter, f, s, var) + if type(filter) ~= "function" then + local filter_value = filter; + function filter(x) return x ~= filter_value; end + end + return function (s, var) + local ret; + repeat ret = pack(f(s, var)); + var = ret[1]; + until var == nil or filter(unpack(ret, 1, ret.n)); + return unpack(ret, 1, ret.n); + end, s, var; end local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end -- cgit v1.2.3 From f42128298646404ddb77cc04ebf11dc68281e98b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 17 May 2013 08:42:21 +0100 Subject: util.iterators: Small fix for variable scoping issue --- util/iterators.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util/iterators.lua') diff --git a/util/iterators.lua b/util/iterators.lua index bcda48b4..4b429163 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -75,7 +75,7 @@ function it.count(f, s, var) local x = 0; while true do - local var = f(s, var); + var = f(s, var); if var == nil then break; end x = x + 1; end -- cgit v1.2.3