aboutsummaryrefslogtreecommitdiffstats
path: root/util/iterators.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2013-05-17 08:30:28 +0100
committerMatthew Wild <mwild1@gmail.com>2013-05-17 08:30:28 +0100
commit61ccc4463ded21290bd964409597bbade9781108 (patch)
tree51ab0b8f5807dee9c5c00de2ed0b3c0df9c619bc /util/iterators.lua
parent42be754bb20ba65af1a3b311b82f5765b8985234 (diff)
downloadprosody-61ccc4463ded21290bd964409597bbade9781108.tar.gz
prosody-61ccc4463ded21290bd964409597bbade9781108.zip
util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Diffstat (limited to 'util/iterators.lua')
-rw-r--r--util/iterators.lua20
1 files changed, 12 insertions, 8 deletions
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