aboutsummaryrefslogtreecommitdiffstats
path: root/util/iterators.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2012-04-29 02:10:55 +0100
committerMatthew Wild <mwild1@gmail.com>2012-04-29 02:10:55 +0100
commitbd5f9b8cbfd168a21141607218446222ab9d8642 (patch)
treee78baabff21e9b6fefaeef4a709098ca30b35922 /util/iterators.lua
parent9b89441dd008a8b3fda12d9217ff6f9ba240df9a (diff)
parenta2055895d362fed58ff314820bb7fddd53e40436 (diff)
downloadprosody-bd5f9b8cbfd168a21141607218446222ab9d8642.tar.gz
prosody-bd5f9b8cbfd168a21141607218446222ab9d8642.zip
Merge 0.9->trunk
Diffstat (limited to 'util/iterators.lua')
-rw-r--r--util/iterators.lua27
1 files changed, 15 insertions, 12 deletions
diff --git a/util/iterators.lua b/util/iterators.lua
index 2a87e97a..fb89f4a5 100644
--- a/util/iterators.lua
+++ b/util/iterators.lua
@@ -8,8 +8,10 @@
--[[ Iterators ]]--
+local it = {};
+
-- Reverse an iterator
-function reverse(f, s, var)
+function it.reverse(f, s, var)
local results = {};
-- First call the normal iterator
@@ -34,12 +36,12 @@ end
local function _keys_it(t, key)
return (next(t, key));
end
-function keys(t)
+function it.keys(t)
return _keys_it, t;
end
-- Iterate only over values in a table
-function values(t)
+function it.values(t)
local key, val;
return function (t)
key, val = next(t, key);
@@ -48,7 +50,7 @@ function values(t)
end
-- Given an iterator, iterate only over unique items
-function unique(f, s, var)
+function it.unique(f, s, var)
local set = {};
return function ()
@@ -65,7 +67,7 @@ function unique(f, s, var)
end
--[[ Return the number of items an iterator returns ]]--
-function count(f, s, var)
+function it.count(f, s, var)
local x = 0;
while true do
@@ -79,7 +81,7 @@ function count(f, s, var)
end
-- Return the first n items an iterator returns
-function head(n, f, s, var)
+function it.head(n, f, s, var)
local c = 0;
return function (s, var)
if c >= n then
@@ -91,7 +93,7 @@ function head(n, f, s, var)
end
-- Skip the first n items an iterator returns
-function skip(n, f, s, var)
+function it.skip(n, f, s, var)
for i=1,n do
var = f(s, var);
end
@@ -99,7 +101,7 @@ function skip(n, f, s, var)
end
-- Return the last n items an iterator returns
-function tail(n, f, s, var)
+function it.tail(n, f, s, var)
local results, count = {}, 0;
while true do
local ret = { f(s, var) };
@@ -121,13 +123,13 @@ function tail(n, f, s, var)
end
local function _range_iter(max, curr) if curr < max then return curr + 1; end end
-function range(x, y)
+function it.range(x, y)
if not y then x, y = 1, x; end -- Default to 1..x if y not given
return _range_iter, y, x-1;
end
-- Convert the values returned by an iterator to an array
-function it2array(f, s, var)
+function it.to_array(f, s, var)
local t, var = {};
while true do
var = f(s, var);
@@ -139,8 +141,8 @@ end
-- Treat the return of an iterator as key,value pairs,
-- and build a table
-function it2table(f, s, var)
- local t, var = {};
+function it.to_table(f, s, var)
+ local t, var2 = {};
while true do
var, var2 = f(s, var);
if var == nil then break; end
@@ -149,3 +151,4 @@ function it2table(f, s, var)
return t;
end
+return it;