diff options
author | Matthew Wild <mwild1@gmail.com> | 2009-08-10 15:07:32 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2009-08-10 15:07:32 +0100 |
commit | bcb552cdc42eb2ed9fe821625a46eccea5c08d47 (patch) | |
tree | 01168a051d0dfd4754e9d33c11c597892aa8520d /util/iterators.lua | |
parent | 20984a8ffbb01ba242e4efaba6a6bffaf04bbc03 (diff) | |
download | prosody-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.lua | 12 |
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 = {}; |