aboutsummaryrefslogtreecommitdiffstats
path: root/util/iterators.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2009-08-10 15:07:32 +0100
committerMatthew Wild <mwild1@gmail.com>2009-08-10 15:07:32 +0100
commitbcb552cdc42eb2ed9fe821625a46eccea5c08d47 (patch)
tree01168a051d0dfd4754e9d33c11c597892aa8520d /util/iterators.lua
parent20984a8ffbb01ba242e4efaba6a6bffaf04bbc03 (diff)
downloadprosody-bcb552cdc42eb2ed9fe821625a46eccea5c08d47.tar.gz
prosody-bcb552cdc42eb2ed9fe821625a46eccea5c08d47.zip
util.iterators: Add head() iterator, to return the first n items
Diffstat (limited to 'util/iterators.lua')
-rw-r--r--util/iterators.lua12
1 files changed, 12 insertions, 0 deletions
diff --git a/util/iterators.lua b/util/iterators.lua
index 08bb729c..6a437dd7 100644
--- a/util/iterators.lua
+++ b/util/iterators.lua
@@ -78,6 +78,18 @@ function count(f, s, var)
return x;
end
+-- Return the first n items an iterator returns
+function head(n, f, s, var)
+ local c = 0;
+ return function (s, var)
+ if c >= n then
+ return nil;
+ end
+ c = c + 1;
+ return f(s, var);
+ end, s;
+end
+
-- Convert the values returned by an iterator to an array
function it2array(f, s, var)
local t, var = {};