Software /
code /
prosody
Diff
util/iterators.lua @ 5581:167c1da54606
util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 17 May 2013 08:30:28 +0100 |
parent | 5468:2660407c3b73 |
child | 5582:7bc2009fdd0c |
line wrap: on
line diff
--- a/util/iterators.lua Thu May 16 14:17:25 2013 +0100 +++ b/util/iterators.lua Fri May 17 08:30:28 2013 +0100 @@ -10,6 +10,10 @@ 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 it.reverse(f, s, var) local results = {}; @@ -19,7 +23,7 @@ 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 @@ -55,12 +59,12 @@ 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; @@ -71,8 +75,7 @@ local x = 0; while true do - local ret = { f(s, var) }; - var = ret[1]; + local var = f(s, var); if var == nil then break; end x = x + 1; end @@ -104,7 +107,7 @@ 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; @@ -117,7 +120,8 @@ 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))); end @@ -139,7 +143,7 @@ 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