diff options
author | Matthew Wild <mwild1@gmail.com> | 2009-08-10 15:46:34 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2009-08-10 15:46:34 +0100 |
commit | e58d6d5d62912ca78c22ea134bc9a4dff30e9b42 (patch) | |
tree | f8dbc823174c482c76de39d3289256e2846f6486 /util | |
parent | bcb552cdc42eb2ed9fe821625a46eccea5c08d47 (diff) | |
download | prosody-e58d6d5d62912ca78c22ea134bc9a4dff30e9b42.tar.gz prosody-e58d6d5d62912ca78c22ea134bc9a4dff30e9b42.zip |
util.iterators: Add tail() iterator, to return the last n items
Diffstat (limited to 'util')
-rw-r--r-- | util/iterators.lua | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/util/iterators.lua b/util/iterators.lua index 6a437dd7..ba33bc80 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -90,6 +90,27 @@ function head(n, f, s, var) end, s; end +function tail(n, f, s, var) + local results, count = {}, 0; + while true do + local ret = { f(s, var) }; + var = ret[1]; + if var == nil then break; end + results[(count%n)+1] = ret; + count = count + 1; + end + + if n > count then n = count; end + + local pos = 0; + return function () + pos = pos + 1; + if pos > n then return nil; end + return unpack(results[((count-1+pos)%n)+1]); + end + --return reverse(head(n, reverse(f, s, var))); +end + -- Convert the values returned by an iterator to an array function it2array(f, s, var) local t, var = {}; |