util.stanza: stanza.error_reply(): Fix to put the correct namespace on <text>
[prosody.git] / util / iterators.lua
index 6a437dd790d0a42aa26039dac02a480455cb4736..ba33bc80b1e5ebb417dc6513ff5eaa9317a46390 100644 (file)
@@ -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 = {};