aboutsummaryrefslogtreecommitdiffstats
path: root/util/iterators.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/iterators.lua')
-rw-r--r--util/iterators.lua83
1 files changed, 58 insertions, 25 deletions
diff --git a/util/iterators.lua b/util/iterators.lua
index dc692d64..aa9c3ec0 100644
--- a/util/iterators.lua
+++ b/util/iterators.lua
@@ -1,15 +1,21 @@
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
---
+--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
--[[ Iterators ]]--
+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 reverse(f, s, var)
+function it.reverse(f, s, var)
local results = {};
-- First call the normal iterator
@@ -17,9 +23,9 @@ function 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
local i,max = 0, #results;
return function (results)
@@ -34,12 +40,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,38 +54,37 @@ 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 ()
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;
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
- local ret = { f(s, var) };
- var = ret[1];
+ var = f(s, var);
if var == nil then break; end
x = x + 1;
end
-
+
return x;
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 +96,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,10 +104,10 @@ 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) };
+ local ret = pack(f(s, var));
var = ret[1];
if var == nil then break; end
results[(count%n)+1] = ret;
@@ -115,26 +120,52 @@ function 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)));
+ --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
+function it.ripairs(t)
+ return _ripairs_iter, t, #t+1;
+end
+
+local function _range_iter(max, curr) if curr < max then return curr + 1; end end
+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);
if var == nil then break; end
- table.insert(t, var);
+ t_insert(t, var);
end
return t;
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
@@ -142,3 +173,5 @@ function it2table(f, s, var)
end
return t;
end
+
+return it;